Pandas Erratic按DateTime列排序

时间:2018-01-08 19:17:57

标签: python pandas sorting datetime

我有一个pandas数据框,并在阅读了一些评论后(谢谢)想澄清一下。

我已将BeginDate列定义为

data['BeginDate'] = pd.to_datetime(data.BeginDate)
data = data.sort_values(['BeginDate'], ascending = [False]) 

通过打印类型(data ['BeginDate'])返回

<class 'pandas.core.series.Series'>

BeginDate           X
11/18/2017 5:00     2780.784
11/18/2017 6:00     2652.312
11/18/2017 7:00     2596.198
11/18/2017 8:00     2573.241
11/18/2017 9:00     2601.293
11/18/2017 10:00    2803.294
11/18/2017 11:00    2964.356
11/18/2017 12:00    3117.072
11/18/2017 13:00    3254.652
11/19/2017 5:00     2446.695
11/19/2017 6:00     2301.853
11/19/2017 7:00     2204.87
11/19/2017 8:00     2188.789
11/19/2017 9:00     2162.051
11/19/2017 10:00    2289.19
11/19/2017 11:00    2317.428
11/19/2017 12:00    2501.4
11/19/2017 13:00    2686.298
11/19/2017 14:00    2889.88
11/19/2017 15:00    3032.894
11/19/2017 16:00    3027.868
11/19/2017 17:00    3077.443
11/19/2017 18:00    3006.568
11/19/2017 19:00    3046.463
11/19/2017 20:00    3085.442
11/19/2017 21:00    3308.473
11/19/2017 22:00    3679.34
11/19/2017 23:00    3664.858
11/20/2017 0:00     3577.343
11/20/2017 1:00     3385.539
11/20/2017 2:00     3146.491
11/20/2017 3:00     2954.23
11/20/2017 4:00     2676.333

我尝试按'BeginDate'列按下列方式对此数据进行排序,如下所示

data = data.sort_values(['BeginDate'], ascending = [False])

然而,通过这样做,它不仅会失败,而且会将数据混杂成一种对我来说毫无意义的数据。

BeginDate           X
11/19/2017 5:00     2446.695
11/19/2017 6:00     2301.853
11/19/2017 7:00     2204.87
11/19/2017 8:00     2188.789
11/19/2017 9:00     2162.051
11/19/2017 10:00    2289.19
11/19/2017 11:00    2317.428
11/19/2017 12:00    2501.4
11/19/2017 13:00    2686.298
11/19/2017 14:00    2889.88
11/19/2017 15:00    3032.894
11/19/2017 16:00    3027.868
11/19/2017 17:00    3077.443
11/19/2017 18:00    3006.568
11/19/2017 19:00    3046.463
11/19/2017 20:00    3085.442
11/19/2017 21:00    3308.473
11/19/2017 22:00    3679.34
11/19/2017 23:00    3664.858
11/20/2017 0:00     3577.343
11/20/2017 1:00     3385.539
11/20/2017 2:00     3146.491
11/20/2017 3:00     2954.23
11/20/2017 4:00     2676.333
11/18/2017 5:00     2780.784
11/18/2017 6:00     2652.312
11/18/2017 7:00     2596.198
11/18/2017 8:00     2573.241
11/18/2017 9:00     2601.293
11/18/2017 10:00    2803.294
11/18/2017 11:00    2964.356
11/18/2017 12:00    3117.072
11/18/2017 13:00    3254.652

我不确定是什么导致这种情况发生,而且我可以告诉我的BeginDate列现在格式正确。在此先感谢您的帮助!

根据评论中的要求,print(data.info())产生

BeginDate                  72 non-null datetime64[ns]
X                          72 non-null float64

0 个答案:

没有答案