cryptodatapy 0.2.26__py3-none-any.whl → 0.2.28__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cryptodatapy/util/utils.py +49 -1
- {cryptodatapy-0.2.26.dist-info → cryptodatapy-0.2.28.dist-info}/METADATA +1 -1
- {cryptodatapy-0.2.26.dist-info → cryptodatapy-0.2.28.dist-info}/RECORD +5 -5
- {cryptodatapy-0.2.26.dist-info → cryptodatapy-0.2.28.dist-info}/LICENSE +0 -0
- {cryptodatapy-0.2.26.dist-info → cryptodatapy-0.2.28.dist-info}/WHEEL +0 -0
cryptodatapy/util/utils.py
CHANGED
@@ -1,4 +1,53 @@
|
|
1
1
|
import pandas as pd
|
2
|
+
from typing import List
|
3
|
+
|
4
|
+
|
5
|
+
def compute_reference_price(dfs: List[pd.DataFrame],
|
6
|
+
method: str = 'median',
|
7
|
+
trim_pct: float = 0.25,
|
8
|
+
) -> pd.DataFrame:
|
9
|
+
"""
|
10
|
+
Computes the consensus price from a list of dataframes.
|
11
|
+
|
12
|
+
Parameters
|
13
|
+
----------
|
14
|
+
dfs: pd.DataFrame
|
15
|
+
List of dataframes containing price data.
|
16
|
+
method: str, optional
|
17
|
+
Method to compute the consensus price. Options are 'median' or 'trimmed_mean'.
|
18
|
+
Default is 'median'.
|
19
|
+
trim_pct: float, optional
|
20
|
+
Percentage of data to trim from both ends for 'trimmed_mean' method.
|
21
|
+
Default is 0.25 (25%).
|
22
|
+
Returns
|
23
|
+
-------
|
24
|
+
pd.DataFrame
|
25
|
+
Dataframe with the consensus price.
|
26
|
+
"""
|
27
|
+
if not List:
|
28
|
+
raise ValueError("The input list is empty.")
|
29
|
+
|
30
|
+
# Concatenate all dataframes in the list
|
31
|
+
stacked_df = pd.concat(dfs)
|
32
|
+
|
33
|
+
# Compute consensus price based on the specified method
|
34
|
+
if method == 'median':
|
35
|
+
consensus_price = stacked_df.groupby(['date', 'ticker']).median()
|
36
|
+
|
37
|
+
elif method == 'trimmed_mean':
|
38
|
+
# Calculate trimmed mean with specified bounds
|
39
|
+
lower_bound = stacked_df.groupby(level=[0, 1]).quantile(trim_pct)
|
40
|
+
upper_bound = stacked_df.groupby(level=[0, 1]).quantile(1 - trim_pct)
|
41
|
+
|
42
|
+
# Filter out values outside the bounds
|
43
|
+
filtered_df = stacked_df[(stacked_df >= lower_bound.reindex(stacked_df.index)) &
|
44
|
+
(stacked_df <= upper_bound.reindex(stacked_df.index))]
|
45
|
+
|
46
|
+
consensus_price = filtered_df.groupby(level=[0, 1]).mean()
|
47
|
+
else:
|
48
|
+
raise ValueError("Method must be either 'median' or 'trimmed_mean'.")
|
49
|
+
|
50
|
+
return consensus_price.sort_index()
|
2
51
|
|
3
52
|
|
4
53
|
def stitch_dataframes(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
|
@@ -53,7 +102,6 @@ def rebase_fx_to_foreign_vs_usd(df) -> pd.DataFrame:
|
|
53
102
|
raise ValueError(f"Unexpected ticker format: {ticker}")
|
54
103
|
|
55
104
|
if isinstance(df.index, pd.MultiIndex):
|
56
|
-
# MultiIndex: (date, ticker)
|
57
105
|
tickers = df.index.get_level_values(1)
|
58
106
|
inverted = tickers.str.startswith("USD")
|
59
107
|
|
@@ -57,8 +57,8 @@ cryptodatapy/transform/wrangle.py,sha256=cQOkPoiOmQtC7d2G15jMbMJSbinMmLYxM6Or7Ff
|
|
57
57
|
cryptodatapy/util/__init__.py,sha256=zSQ2HU2QIXzCuptJjknmrClwtQKCvIj4aNysZljIgrU,116
|
58
58
|
cryptodatapy/util/datacatalog.py,sha256=qCCX6srXvaAbVAKuA0M2y5IK_2OEx5xA3yRahDZlC-g,13157
|
59
59
|
cryptodatapy/util/datacredentials.py,sha256=BnoQlUchbP0vfXqXRuhCOOsHyUTMuH5T4RAKBbHzMyo,3140
|
60
|
-
cryptodatapy/util/utils.py,sha256=
|
61
|
-
cryptodatapy-0.2.
|
62
|
-
cryptodatapy-0.2.
|
63
|
-
cryptodatapy-0.2.
|
64
|
-
cryptodatapy-0.2.
|
60
|
+
cryptodatapy/util/utils.py,sha256=zXDPnqLNaeWVGgUDVG70q-WcaIuxbAVbYeinKchdlL4,4146
|
61
|
+
cryptodatapy-0.2.28.dist-info/LICENSE,sha256=sw4oVq8bDjT3uMtaFebQ-xeIVP4H-bXldTs9q-Jjeks,11344
|
62
|
+
cryptodatapy-0.2.28.dist-info/METADATA,sha256=or2VOUWh0yvQGwJ-v3NPPWqNJpWR1Di7tDiSQ-5rTIo,6473
|
63
|
+
cryptodatapy-0.2.28.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
64
|
+
cryptodatapy-0.2.28.dist-info/RECORD,,
|
File without changes
|
File without changes
|