问题:
处理市场数据并将日内数据重新采样到每日时间范围时,如下所示:
ohlc_dict = {
'Open':'first',
'High':'max',
'Low':'min',
'Last': 'last',
'Volume': 'sum'}
data.resample('1D',how=ohlc_dict).tail().dropna()
Open High Last Low Volume
Timestamp
2016-12-27 163.55 164.18 164.11 163.55 144793.00
2016-12-28 164.18 164.33 164.22 163.89 215288.00
2016-12-29 164.44 164.65 164.49 164.27 245538.00
2016-12-30 164.55 164.56 164.18 164.09 286847.00
Run Code Online (Sandbox Code Playgroud)
这似乎给了我需要的输出(仍需要验证)...
我收到以下警告:
FutureWarning: how in .resample() is deprecated
the new syntax is .resample(...)..apply(<func>)
Run Code Online (Sandbox Code Playgroud)
题:
如何resample使用新语法复制此代码以与当前使用的最佳实践保持一致apply?
我尝试过的:
仅使用数据['低']作为示例:
def ohlc (df):
return df['Low'].min()
data.resample('1D').dropna().apply(ohlc,axis=1).tail(2)
Timestamp
2016-12-29 164.45
2016-12-30 164.26
dtype: float64
Run Code Online (Sandbox Code Playgroud)
不给我相同的结果,我不知道在哪里插入apply. …
我知道如何在查询单个列时创建一个掩码来过滤数据帧:
import pandas as pd
import datetime
index = pd.date_range('2013-1-1',periods=100,freq='30Min')
data = pd.DataFrame(data=list(range(100)), columns=['value'], index=index)
data['value2'] = 'A'
data['value2'].loc[0:10] = 'B'
data
value value2
2013-01-01 00:00:00 0 B
2013-01-01 00:30:00 1 B
2013-01-01 01:00:00 2 B
2013-01-01 01:30:00 3 B
2013-01-01 02:00:00 4 B
2013-01-01 02:30:00 5 B
2013-01-01 03:00:00 6 B
Run Code Online (Sandbox Code Playgroud)
我在这里使用一个简单的面具:
mask = data['value'] > 4
data[mask]
value value2
2013-01-01 02:30:00 5 B
2013-01-01 03:00:00 6 B
2013-01-01 03:30:00 7 B
2013-01-01 04:00:00 8 B
2013-01-01 04:30:00 9 …Run Code Online (Sandbox Code Playgroud) 题:
我想更好地理解Pandas DataFrame.query方法以及以下表达式代表的内容:
match = dfDays.query('index > @x.name & price >= @x.target')
Run Code Online (Sandbox Code Playgroud)
什么是@x.name代表?
我理解这个代码(带有pandas.tslib.Timestamp数据的新列)的结果输出是什么,但是没有清楚地理解用于获得此最终结果的表达式.
数据:
从这里:
np.random.seed(seed=1)
rng = pd.date_range('1/1/2000', '2000-07-31',freq='D')
weeks = np.random.uniform(low=1.03, high=3, size=(len(rng),))
ts2 = pd.Series(weeks
,index=rng)
dfDays = pd.DataFrame({'price':ts2})
dfWeeks = dfDays.resample('1W-Mon').first()
dfWeeks['target'] = (dfWeeks['price'] + .5).round(2)
def find_match(x):
match = dfDays.query('index > @x.name & price >= @x.target')
if not match.empty:
return match.index[0]
dfWeeks.assign(target_hit=dfWeeks.apply(find_match, 1))
Run Code Online (Sandbox Code Playgroud) 如何通过数值对此列表进行排序?是正则表达式需要删除数字还是有更多的Pythonic方法来做到这一点?
to_sort
['12-foo',
'1-bar',
'2-bar',
'foo-11',
'bar-3',
'foo-4',
'foobar-5',
'6-foo',
'7-bar']
Run Code Online (Sandbox Code Playgroud)
期望的输出如下:
1-bar
2-bar
bar-3
foo-4
foobar-5
6-foo
7-bar
foo-11
12-foo
Run Code Online (Sandbox Code Playgroud) 这篇文章展示了如何使用first_valid_index查找数据帧列中第一次出现的值。如何将first_valid_index与每日groupby一起使用,以便找到链接帖子中所示的相同示例数据帧每天的第一次出现?
这是我需要使用的 groupby 代码:
grouper = pd.TimeGrouper("1d")
Run Code Online (Sandbox Code Playgroud)
编辑:
当我使用lambdaandapply方法时,它会给出正确的输出。我无法将此输出发送到新列,['test_output']因为它只显示 NaT:
df['test_output'] = df.groupby(grouper)['test_1'].apply(lambda x: x.first_valid_index())
df
Out[9]:
test_1 test_output
2014-03-04 09:00:00 NaN NaT
2014-03-04 10:00:00 NaN NaT
2014-03-04 11:00:00 NaN NaT
2014-03-04 12:00:00 NaN NaT
2014-03-04 13:00:00 NaN NaT
2014-03-04 14:00:00 1.0 NaT
2014-03-04 15:00:00 1.0 NaT
2014-03-04 16:00:00 1.0 NaT
2014-03-05 09:00:00 1.0 NaT
Run Code Online (Sandbox Code Playgroud) 运行以下代码时:
for row,hit in hits.iterrows():
forwardRows = data[data.index.values > row];
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TypeError: Cannot compare type 'Timestamp' with type 'int'
Run Code Online (Sandbox Code Playgroud)
如果我查看这里比较的内容,我有以下变量:
type(row)
pandas.tslib.Timestamp
row
Timestamp('2015-09-01 09:30:00')
Run Code Online (Sandbox Code Playgroud)
被比较:
type(data.index.values[0])
numpy.datetime64
data.index.values[0]
numpy.datetime64('2015-09-01T10:30:00.000000000+0100')
Run Code Online (Sandbox Code Playgroud)
我想了解这是否可以轻松修复,还是应该上传我的数据子集?谢谢
我正在尝试研究如何显示熊猫系列中的 条纹True或 条纹。False
数据:
p = pd.Series([True,False,True,True,True,True,False,False,True])
0 True
1 False
2 True
3 True
4 True
5 True
6 False
7 False
8 True
dtype: bool
Run Code Online (Sandbox Code Playgroud)
我尝试过p.diff(),但不确定如何计算False生成的值以显示我想要的输出,如下所示:
0 0
1 0
2 0
3 1
4 2
5 3
6 0
7 1
8 0
Run Code Online (Sandbox Code Playgroud) 题:
概述:
我正在寻找一种矢量化方式来获得第一个看到某种情况的日期.当价格dfDays为>指定的目标价格时,会找到该条件dfWeeks.target.必须在设置目标之后触发此条件.
有没有apply办法在Pandas中以矢量化的方式进行以下时间序列分析?
数据:
生成freq='D'测试数据帧
np.random.seed(seed=1)
rng = pd.date_range('1/1/2000', '2000-07-31',freq='D')
weeks = np.random.uniform(low=1.03, high=3, size=(len(rng),))
ts2 = pd.Series(weeks
,index=rng)
dfDays = pd.DataFrame({'price':ts2})
Run Code Online (Sandbox Code Playgroud)
现在创建一个重新采样的freq='1W-Mon'数据帧
dfWeeks = dfDays.resample('1W-Mon').first()
dfWeeks['target'] = (dfWeeks['price'] + .5).round(2)
Run Code Online (Sandbox Code Playgroud)
用于reindex在两个df上对齐索引:
dfWeeks = dfWeeks.reindex(dfDays.index)
Run Code Online (Sandbox Code Playgroud)
dfWeeks包含我们将使用的目标值的数据框也是如此
dfWeeks.dropna().head()
price target
2000-01-03 1.851533 2.35
2000-01-10 1.625595 2.13
2000-01-17 1.855813 2.36
2000-01-24 2.130619 2.63
2000-01-31 2.756487 3.26
Run Code Online (Sandbox Code Playgroud)
如果我们专注于第一个目标 dfWeeks
match = dfDays[dfDays.price >= dfWeeks.target.loc['2000-01-03']]
Run Code Online (Sandbox Code Playgroud)
第一场比赛是过去的,所以无效,所以2000-01-12参赛作品是第一场有效比赛: …
我正在尝试使用 Plotly 在 OHLC 蜡烛图之上覆盖标记/折线图。
代码
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
df = pd.DataFrame(
{'index': {0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 11,
12: 12,
13: 13,
14: 14,
15: 15,
16: 16,
17: 17,
18: 18,
19: 19,
20: 20,
21: 21,
22: 22,
23: 23,
24: 24},
'Date': {0: '2018-09-03',
1: '2018-09-04',
2: '2018-09-05',
3: …Run Code Online (Sandbox Code Playgroud) 该gcloud auth print-identity-token命令打印指定帐户的身份令牌。
$(gcloud auth print-identity-token \
--audiences=https://example.com \
--impersonate-service-account my-sa@my-project.iam.gserviceaccount.com \
--include-email)
Run Code Online (Sandbox Code Playgroud)
我如何使用 Python 来做同样的事情?