如何使用修改后的索引来旋转 pandas 数据框?
How to pivot a pandas dataframe using a modified index?
我有一个时间序列数据框,格式为:
rng = pd.date_range('1/1/2013', periods=1000, freq='10min')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
ts = ts.to_frame(name=None)
我需要对它做两件事:
第一步:修改索引,使每一天都从前一天的17:00:00开始。我这样做使用:
ts.index = pd.to_datetime(ts.index.values + np.where((ts.index.time >= datetime.time(17)), pd.offsets.Day(1).nanos, 0))
第 2 步: 旋转数据框,如下所示:
ts_ = pd.pivot_table(ts, index=ts.index.date, columns=ts.index.time, values=0)
我遇到的问题是,在旋转数据帧时,pandas 似乎忘记了我在第 1 步中对索引所做的修改。
这就是我得到的
00:00:00 00:10:00 00:20:00 ... 23:50:00
2013-01-10 -1.800381 -0.459226 -0.172929 ... -1.000381
2013-01-11 -1.258317 -0.973924 0.955224 ... 0.072929
2013-01-12 -0.834976 0.018793 -0.141608 ... 2.072929
2013-01-13 -0.131197 0.289998 2.200644 ... 1.589998
2013-01-14 -0.991653 0.276874 -1.390654 ... -2.090654
相反,这是期望的结果
17:00:00 17:10:00 17:20:00 ... 16:50:00
2013-01-10 -2.800381 1.000226 2.172929 ... 0.172929
2013-01-11 0.312587 1.003924 2.556624 ... -0.556624
2013-01-12 2.976834 1.000003 -2.141608 ... -1.141608
2013-01-13 1.197131 1.333998 -2.999944 ... -1.999944
2013-01-14 -1.653991 1.278884 -1.390654 ... -4.390654
编辑 - 说明说明:请注意每天从“17:00:00”开始到“16:50:00”结束是多么理想。
使用 Python 2.7
注意: Nickil Maveli 提出的解决方案近似于答案,但以错误的方式移动了日期。这个想法是 Day_t = 开始于 Day_t-1 '17:00'。现在,解决方案正在做 Day_t = Starts at Day_t at '17:00'.
你真的不需要在这里使用 np.where
因为你只是对 1 个参数执行过滤。还有就是把else
部分设为0,所以经过这一步得到的索引绝对没有减少。
相反,您必须这样做:
1.Build 向上一个布尔掩码来过滤 hour
属性大于或等于 17 并添加一天的偏移量的日期时间:
arr = ts.index
idx = arr[arr.hour >= 17] + pd.offsets.Day(1)
2.Reindex 基于修改索引:
ts_clip = ts.reindex(idx)
3.Perform pivot
操作:
pd.pivot_table(ts_clip, index=ts_clip.index.date, columns=ts_clip.index.time, values=0)
编辑
ts_clip = ts.iloc[np.argwhere(ts.index.hour.__eq__(17)).ravel()[0]:]
ts_clip_shift = ts_clip.tshift(-17, freq='H')
df = pd.pivot_table(ts_clip_shift, index=(ts_clip_shift.index + pd.offsets.Day(n=1)),
columns=ts_clip_shift.index.time, values=0)
df.columns= ts_clip.iloc[:len(df.columns)].index.time
检查DF
特征:
df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 7 entries, 2013-01-02 to 2013-01-08
Columns: 144 entries, 17:00:00 to 16:50:00
dtypes: float64(144)
memory usage: 7.9+ KB
所以我需要画一些图,所以here它们是:
# Step 1:
df1 = df.ix[:, :'16:59'] # http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ix.html
df2 = df.ix[:, '17:00' : ]
# Step 2:
df3 = df2.shift(periods = 1) # http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.shift.html
# Step 3:
df4 = pandas.concat([df3, df1], axis = 1) # http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html
我有一个时间序列数据框,格式为:
rng = pd.date_range('1/1/2013', periods=1000, freq='10min')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
ts = ts.to_frame(name=None)
我需要对它做两件事:
第一步:修改索引,使每一天都从前一天的17:00:00开始。我这样做使用:
ts.index = pd.to_datetime(ts.index.values + np.where((ts.index.time >= datetime.time(17)), pd.offsets.Day(1).nanos, 0))
第 2 步: 旋转数据框,如下所示:
ts_ = pd.pivot_table(ts, index=ts.index.date, columns=ts.index.time, values=0)
我遇到的问题是,在旋转数据帧时,pandas 似乎忘记了我在第 1 步中对索引所做的修改。
这就是我得到的
00:00:00 00:10:00 00:20:00 ... 23:50:00
2013-01-10 -1.800381 -0.459226 -0.172929 ... -1.000381
2013-01-11 -1.258317 -0.973924 0.955224 ... 0.072929
2013-01-12 -0.834976 0.018793 -0.141608 ... 2.072929
2013-01-13 -0.131197 0.289998 2.200644 ... 1.589998
2013-01-14 -0.991653 0.276874 -1.390654 ... -2.090654
相反,这是期望的结果
17:00:00 17:10:00 17:20:00 ... 16:50:00
2013-01-10 -2.800381 1.000226 2.172929 ... 0.172929
2013-01-11 0.312587 1.003924 2.556624 ... -0.556624
2013-01-12 2.976834 1.000003 -2.141608 ... -1.141608
2013-01-13 1.197131 1.333998 -2.999944 ... -1.999944
2013-01-14 -1.653991 1.278884 -1.390654 ... -4.390654
编辑 - 说明说明:请注意每天从“17:00:00”开始到“16:50:00”结束是多么理想。
使用 Python 2.7
注意: Nickil Maveli 提出的解决方案近似于答案,但以错误的方式移动了日期。这个想法是 Day_t = 开始于 Day_t-1 '17:00'。现在,解决方案正在做 Day_t = Starts at Day_t at '17:00'.
你真的不需要在这里使用 np.where
因为你只是对 1 个参数执行过滤。还有就是把else
部分设为0,所以经过这一步得到的索引绝对没有减少。
相反,您必须这样做:
1.Build 向上一个布尔掩码来过滤 hour
属性大于或等于 17 并添加一天的偏移量的日期时间:
arr = ts.index
idx = arr[arr.hour >= 17] + pd.offsets.Day(1)
2.Reindex 基于修改索引:
ts_clip = ts.reindex(idx)
3.Perform pivot
操作:
pd.pivot_table(ts_clip, index=ts_clip.index.date, columns=ts_clip.index.time, values=0)
编辑
ts_clip = ts.iloc[np.argwhere(ts.index.hour.__eq__(17)).ravel()[0]:]
ts_clip_shift = ts_clip.tshift(-17, freq='H')
df = pd.pivot_table(ts_clip_shift, index=(ts_clip_shift.index + pd.offsets.Day(n=1)),
columns=ts_clip_shift.index.time, values=0)
df.columns= ts_clip.iloc[:len(df.columns)].index.time
检查DF
特征:
df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 7 entries, 2013-01-02 to 2013-01-08
Columns: 144 entries, 17:00:00 to 16:50:00
dtypes: float64(144)
memory usage: 7.9+ KB
所以我需要画一些图,所以here它们是:
# Step 1:
df1 = df.ix[:, :'16:59'] # http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ix.html
df2 = df.ix[:, '17:00' : ]
# Step 2:
df3 = df2.shift(periods = 1) # http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.shift.html
# Step 3:
df4 = pandas.concat([df3, df1], axis = 1) # http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html