我有一个特定股票报价器近一年的分钟数据,我对其进行了屏蔽,以返回一个Pandas系列的布尔值,无论EMA是否越过了另一个,例如:
print(entries)
print(type(entries))
print(f'len of entries {len(entries)}')
输出:
datetime
2021-05-03 08:00:00 False
2021-05-03 08:03:00 False
2021-05-03 08:04:00 False
2021-05-03 08:06:00 False
2021-05-03 08:08:00 False
2021-05-03 08:09:00 False
2021-05-03 08:11:00 False
2021-05-03 08:29:00 False
2021-05-03 08:34:00 False
2021-05-03 08:41:00 False
2021-05-03 08:52:00 False
2021-05-03 08:55:00 False
2021-05-03 09:07:00 False
2021-05-03 09:13:00 False
2021-05-03 09:14:00 False
2021-05-03 09:26:00 False
2021-05-03 09:27:00 False
2021-05-03 09:34:00 False
2021-05-03 09:44:00 False
2021-05-03 09:57:00 False
2021-05-03 10:14:00 False
2021-05-03 10:31:00 False
2021-05-03 10:38:00 False
2021-05-03 10:41:00 False
...
2022-03-02 23:49:00 False
dtype: bool
<class 'pandas.core.series.Series'>
len of entries 141615
然后我得到了我感兴趣的当天,以及930-1600之间的时间:
display(df_market_hours)
print(type(df_market_hours))
print(f'len of df_market_hours {len(df_market_hours)}')
输出:
volume open close high low time date
datetime
2021-08-13 09:30:00 200.0 15.0000 15.0000 15.00 15.0000 09:30:00 2021-08-13
2021-08-13 10:01:00 310.0 15.0000 15.0000 15.00 15.0000 10:01:00 2021-08-13
2021-08-13 10:26:00 1031.0 14.9000 14.8900 14.90 14.8900 10:26:00 2021-08-13
2021-08-13 10:29:00 223.0 14.8900 14.8900 14.89 14.8900 10:29:00 2021-08-13
2021-08-13 11:00:00 285.0 14.8900 14.8900 14.89 14.8900 11:00:00 2021-08-13
... ... ... ... ... ... ... ...
2021-08-13 15:55:00 11489.0 18.0200 18.0100 18.04 17.9700 15:55:00 2021-08-13
2021-08-13 15:56:00 28828.0 18.0400 17.9700 18.18 17.9300 15:56:00 2021-08-13
2021-08-13 15:57:00 5720.0 17.9600 18.0412 18.05 17.9600 15:57:00 2021-08-13
2021-08-13 15:58:00 33329.0 18.0401 18.1800 18.31 18.0294 15:58:00 2021-08-13
2021-08-13 15:59:00 51872.0 18.1400 18.1650 18.18 18.0600 15:59:00 2021-08-13
184 rows × 7 columns
<class 'pandas.core.frame.DataFrame'>
len of df_market_hours 184
我要做的是返回一个新的系列,其中entries
具有与df_market_hours
相同的索引和形状。我该如何做到这一点?
我已经试过了:
获取当前会话时间的索引:
df_market_hours_idx = df_current_day.between_time('9:30', '16:00', include_end=False).index
df_market_hours = df_current_day.loc[df_market_hours_idx]
然后尝试用它创建一个蒙版:
df_copy = entries[entries == df_market_hours.index]
当我这样做时,我得到以下错误:
ValueError: ('Lengths must match to compare', (141615,), (54991,))
我想我需要使用reindex
和fill_value
,就像这里显示的,但我正在努力弄清楚这如何适用于我的用例
我不太确定理解这个问题,但如果你想保持原始pd.DataFrame
的初始索引,同时提取pd.Series
?也许你可以使用pd.Series()
函数中的索引选项。
编辑:
您可以做的是创建条目列表,然后使用isin()函数进行过滤,以提取df_market_hours
中的相关数据:
lst_entries = list(entries)
masked_df_market_hours = df_market_hours[df_market_hours["datetime"].isin(lst_entries)]
据我所知,您想通过df_market_hours
索引过滤entries
数据帧
considered_indice = df_market_hours.index.values.tolist()
mask = entries.index.isin(considered_indice)
df_copy = entries[mask]