Pandas resample不会返回任何内容

时间:2017-03-08 21:59:22

标签: python pandas

我正在学习使用pandas resample()函数,但是,以下代码不会按预期返回任何内容。我白天重新抽样时间序列。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

range = pd.date_range('2015-01-01','2015-12-31',freq='15min')
df = pd.DataFrame(index = range)

df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
df['distance'] = df['speed'] * 0.25
df['cumulative_distance'] = df.distance.cumsum()

print df.head()

weekly_summary = pd.DataFrame()
weekly_summary['speed'] = df.speed.resample('D').mean()
weekly_summary['distance'] = df.distance.resample('D').sum()

print weekly_summary.head()

输出

                     speed  distance  cumulative_distance
2015-01-01 00:00:00     40     10.00                10.00
2015-01-01 00:15:00      6      1.50                11.50
2015-01-01 00:30:00     31      7.75                19.25
2015-01-01 00:45:00     41     10.25                29.50
2015-01-01 01:00:00     59     14.75                44.25

[5 rows x 3 columns]
Empty DataFrame
Columns: [speed, distance]
Index: []

[0 rows x 2 columns]

2 个答案:

答案 0 :(得分:1)

根据您的熊猫版本,您将如何执行此操作会有所不同。

在pandas 0.19.0中,您的代码按预期工作:

In [7]: pd.__version__
Out[7]: '0.19.0'

In [8]: df.speed.resample('D').mean().head()
Out[8]: 
2015-01-01    28.562500
2015-01-02    30.302083
2015-01-03    30.864583
2015-01-04    29.197917
2015-01-05    30.708333
Freq: D, Name: speed, dtype: float64

在旧版本中,您的解决方案可能不起作用,但至少在0.14.1中,您可以调整它来执行此操作:

>>> pd.__version__
'0.14.1'
>>> df.speed.resample('D').mean()
29.41087328767123
>>> df.speed.resample('D', how='mean').head()
2015-01-01    29.354167
2015-01-02    26.791667
2015-01-03    31.854167
2015-01-04    26.593750
2015-01-05    30.312500
Freq: D, Name: speed, dtype: float64

答案 1 :(得分:1)

这看起来像旧版熊猫的问题,在较新版本中,它会在分配索引不同形状的新列时放大df。应该做的是不要制作一个空的df,而是将初始调用传递给resample作为df ctor的数据arg:

In [8]:
range = pd.date_range('2015-01-01','2015-12-31',freq='15min')
df = pd.DataFrame(index = range)
df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
df['distance'] = df['speed'] * 0.25
df['cumulative_distance'] = df.distance.cumsum()
print (df.head())
weekly_summary = pd.DataFrame(df.speed.resample('D').mean())
weekly_summary['distance'] = df.distance.resample('D').sum()
print( weekly_summary.head())

                     speed  distance  cumulative_distance
2015-01-01 00:00:00     28       7.0                  7.0
2015-01-01 00:15:00      8       2.0                  9.0
2015-01-01 00:30:00     10       2.5                 11.5
2015-01-01 00:45:00     56      14.0                 25.5
2015-01-01 01:00:00      6       1.5                 27.0
                speed  distance
2015-01-01  27.895833    669.50
2015-01-02  29.041667    697.00
2015-01-03  27.104167    650.50
2015-01-04  28.427083    682.25
2015-01-05  27.854167    668.50

在这里,我将调用传递给resample作为df ctor的数据arg,这将获取索引和列名称并创建单个列df:

weekly_summary = pd.DataFrame(df.speed.resample('D').mean())

然后后续作业应按预期工作