Pandas pd.cut() - binning datetime列/系列

Art*_*and 6 datetime binning dataframe python-3.x pandas

尝试使用pd.cut()进行bin,但它相当精细 -

一位同事给我发送了多个报告日期的文件,例如:

 '03-16-2017 to 03-22-2017'
 '03-23-2017 to 03-29-2017'
 '03-30-2017 to 04-05-2017'
Run Code Online (Sandbox Code Playgroud)

它们全部合并为一个数据帧,并给出一个列名,df ['Filedate'],以便文件中的每个记录都有正确的归档.

最后一天是一个截止点,所以我创建了一个新列df ['Filedate_bin'],它将最后一天转换为2017年3月2日,2017年3月29日,2017年4月5日作为字符串.

然后我创建了一个列表:Filedate_bin_list = df.Filedate_bin.unique().因此,我有一个唯一的字符串截止日期列表,我想用作垃圾箱.

将不同的数据导入数据框,有一列交易日期:2017年3月28日,3月29日,3/30/2017,4/1/2017,4/2/2,等等.将它们分配给bin很难,它尝试过:

df['bin'] = pd.cut(df.Processed_date, Filedate_bin_list)
Run Code Online (Sandbox Code Playgroud)

收到 TypeError: unsupported operand type for -: 'str' and 'str'

退回并尝试将Filedate_bin转换为datetime,格式='%m /%d /%Y'并获取

TypeError: Cannot cast ufunc less input from dtype('<m8[ns]') to dtype ('<m8') with casting rule 'same_kind'.

有没有更好的方法将我的processed_date绑定到文本箱?

我试图在我的处理日期2017年3月27日到'03 -23-2017到03-29-2017'

Max*_*axU 10

考虑这种方法:

df = pd.DataFrame(pd.date_range('2000-01-02', freq='1D', periods=15), columns=['Date'])

bins_dt = pd.date_range('2000-01-01', freq='3D', periods=6)
bins_str = bins_dt.astype(str).values

labels = ['({}, {}]'.format(bins_str[i-1], bins_str[i]) for i in range(1, len(bins_str))]

df['cat'] = pd.cut(df.Date.astype(np.int64)//10**9,
                   bins=bins_dt.astype(np.int64)//10**9,
                   labels=labels)
Run Code Online (Sandbox Code Playgroud)

结果:

In [59]: df
Out[59]:
         Date                       cat
0  2000-01-02  (2000-01-01, 2000-01-04]
1  2000-01-03  (2000-01-01, 2000-01-04]
2  2000-01-04  (2000-01-01, 2000-01-04]
3  2000-01-05  (2000-01-04, 2000-01-07]
4  2000-01-06  (2000-01-04, 2000-01-07]
5  2000-01-07  (2000-01-04, 2000-01-07]
6  2000-01-08  (2000-01-07, 2000-01-10]
7  2000-01-09  (2000-01-07, 2000-01-10]
8  2000-01-10  (2000-01-07, 2000-01-10]
9  2000-01-11  (2000-01-10, 2000-01-13]
10 2000-01-12  (2000-01-10, 2000-01-13]
11 2000-01-13  (2000-01-10, 2000-01-13]
12 2000-01-14  (2000-01-13, 2000-01-16]
13 2000-01-15  (2000-01-13, 2000-01-16]
14 2000-01-16  (2000-01-13, 2000-01-16]

In [60]: df.dtypes
Out[60]:
Date    datetime64[ns]
cat           category
dtype: object
Run Code Online (Sandbox Code Playgroud)

说明:

pd.cut- 将pd.qcut值转换为UNIX纪元(时间戳 - 自秒以来的秒数df.Date.astype(np.int64)//10**9):

In [65]: df.Date.astype(np.int64)//10**9
Out[65]:
0     946771200
1     946857600
2     946944000
3     947030400
4     947116800
5     947203200
6     947289600
7     947376000
8     947462400
9     947548800
10    947635200
11    947721600
12    947808000
13    947894400
14    947980800
Name: Date, dtype: int64
Run Code Online (Sandbox Code Playgroud)

同样适用于datetime:

In [66]: bins_dt.astype(np.int64)//10**9
Out[66]: Int64Index([946684800, 946944000, 947203200, 947462400, 947721600, 947980800], dtype='int64')
Run Code Online (Sandbox Code Playgroud)

标签:

In [67]: labels
Out[67]:
['(2000-01-01, 2000-01-04]',
 '(2000-01-04, 2000-01-07]',
 '(2000-01-07, 2000-01-10]',
 '(2000-01-10, 2000-01-13]',
 '(2000-01-13, 2000-01-16]']
Run Code Online (Sandbox Code Playgroud)