pandas基于object和datetime列对DataFrame进行排序?

时间:2013-07-24 11:30:16

标签: python datetime pandas

我只是先用一个带字符串的列然后按datetime列排序pandas数据帧。这样做时,返回的日期不正常。我做错了什么?

df看起来像

                  Date  Field 1
0  2013-07-01 00:00:00        1
1  2013-07-02 00:00:00        1
2  2013-07-03 00:00:00        1
3  2013-07-03 00:00:00        2
4  2013-07-05 00:00:00        2
5  2013-07-05 00:00:00        1
6  2013-07-08 00:00:00        2
7  2013-07-09 00:00:00        2
8  2013-07-11 00:00:00        2
9  2013-07-12 00:00:00        2
10 2013-07-15 00:00:00        1
11 2013-07-16 00:00:00        1
12 2013-07-17 00:00:00        1
13 2013-07-18 00:00:00        1
14 2013-07-19 00:00:00        1

创建数据框时,Date是一个对象,并使用:

转换为datetime
df['Date'] = df['Date'].apply(dateutil.parser.parse)

现在dtypes是:

Date       datetime64[ns]
Field 1             int64
dtype: object
运行

df.sort_index(by=['Field 1', 'Date'])

df.sort(['Field 1','Date'])

我回来了:

                  Date  Field 1
0  2013-07-01 00:00:00        1
1  2013-07-02 00:00:00        1
2  2013-07-03 00:00:00        1
10 2013-07-15 00:00:00        1
5  2013-07-05 00:00:00        1
11 2013-07-16 00:00:00        1
12 2013-07-17 00:00:00        1
13 2013-07-18 00:00:00        1
14 2013-07-19 00:00:00        1
8  2013-07-11 00:00:00        2
9  2013-07-12 00:00:00        2
3  2013-07-03 00:00:00        2
4  2013-07-05 00:00:00        2
6  2013-07-08 00:00:00        2
7  2013-07-09 00:00:00        2

我真正想要的是:

                  Date  Field 1
0  2013-07-01 00:00:00        1
1  2013-07-02 00:00:00        1
2  2013-07-03 00:00:00        1
5  2013-07-05 00:00:00        1
10 2013-07-15 00:00:00        1
11 2013-07-16 00:00:00        1
12 2013-07-17 00:00:00        1
13 2013-07-18 00:00:00        1
14 2013-07-19 00:00:00        1
3  2013-07-03 00:00:00        2
4  2013-07-05 00:00:00        2
6  2013-07-08 00:00:00        2
7  2013-07-09 00:00:00        2
8  2013-07-11 00:00:00        2
9  2013-07-12 00:00:00        2

0 个答案:

没有答案