我正在尝试使用
我想按照自定义日期范围对
我希望将
groupby
和 cut
对日期进行自定义范围分组,但目前没有成功。从返回的错误信息来看,我想知道是不是 cut
正试图将我的日期处理为数字。我想按照自定义日期范围对
df1['date']
进行分组,然后对 df1['HDD']
值求和。自定义范围在 df2
中找到:import pandas as pd
df1 = pd.DataFrame( {'date': ['2/1/2015', '3/2/2015', '3/3/2015', '3/4/2015','4/17/2015','5/12/2015'],
'HDD' : ['7.5','8','5','23','11','55']})
HDD date
0 7.5 2/1/2015
1 8 3/2/2015
2 5 3/3/2015
3 23 3/4/2015
4 11 4/17/2015
5 55 5/12/2015
df2
有自定义日期范围:
df2 = pd.DataFrame( {'Period': ['One','Two','Three','Four'],
'Start Dates': ['1/1/2015','2/15/2015','3/14/2015','4/14/2015'],
'End Dates' : ['2/14/2015','3/13/2015','4/13/2015','5/10/2015']})
Period Start Dates End Dates
0 One 1/1/2015 2/14/2015
1 Two 2/15/2015 3/13/2015
2 Three 3/14/2015 4/13/2015
3 Four 4/14/2015 5/10/2015
我希望将
df1
按照自定义日期范围分组,并对每个时期的HDD值进行聚合。输出应类似于以下内容: Period HDD
0 One 7.5
1 Two 36
2 Three 0
3 Four 11
以下是我尝试使用自定义分组的一个例子:
df3 = df1.groupby(pd.cut(df1['date'], df2['Start Dates'])).agg({'HDD': sum})
...这里是我遇到的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-103-55ea779bcd73> in <module>()
----> 1 df3 = df1.groupby(pd.cut(df1['date'], df2['Start Dates'])).agg({'HDD': sum})
/opt/conda/lib/python3.5/site-packages/pandas/tools/tile.py in cut(x, bins, right, labels, retbins, precision, include_lowest)
112 else:
113 bins = np.asarray(bins)
--> 114 if (np.diff(bins) < 0).any():
115 raise ValueError('bins must increase monotonically.')
116
/opt/conda/lib/python3.5/site-packages/numpy/lib/function_base.py in diff(a, n, axis)
1576 return diff(a[slice1]-a[slice2], n-1, axis=axis)
1577 else:
-> 1578 return a[slice1]-a[slice2]
1579
1580
TypeError: unsupported operand type(s) for -: 'str' and 'str'
- 是否cut试图将我的日期范围处理为数字?
- 我是否需要显式地将我的日期转换为datetime对象(尝试过这样做,但可能不正确)?
感谢提供任何建议!
df1
和df2
)是从外部源导入的,因此我没有创建它们不同的选项。除非我误解了你的建议。 - Adriendf1
中创建'Period'变量,就像这里的人们所做的那样(https://dev59.com/IFYN5IYBdhLWcg3w_84w),然后进行聚合。 - pe-perry