简体   繁体   English

Python 日期顺序错误

[英]Python dates in wrong order

I was wondering why the x-axis plots the dates wrong, it begins at the 05/02 when it should start at the 30/01, and I'm not sure where it is I went wrong.我想知道为什么 x 轴绘制的日期错误,它从 05/02 开始,而它应该从 30/01 开始,我不确定我哪里出错了。

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

cols = ['Time','Water Usage']
A = pd.read_csv("CSVFile", names=cols, parse_dates=[0])

plt.ylabel = "Time"
plt.xlabel = "Water Usage"
A.plot(x='Time',y='Water Usage')
plt.show()

The file is in the format该文件的格式为

30/01/2018 16:00:00 , 50091

05/02/2018 14:00:00, 50890

so ideally it should plot the 30/01 first followed by the 05/02, whereas currently its doing the opposite.所以理想情况下它应该 plot 30/01 首先是 05/02,而目前它的做法相反。

If you just need to reorder it in timely order, You can simply sort dataframe prior to plotting.如果您只是需要及时重新排序,您可以在绘图前简单地排序dataframe。 You can use:您可以使用:

A = A.sort_index()

if the date column is set to be index.如果日期列设置为索引。 If not then following will do the trick:如果没有,那么以下将起到作用:

A = A.set_index('date').sort_index().reset_index()

Since the index is of datetime type, it will automatically sort the whole dataframe由于索引是日期时间类型,它会自动排序整个dataframe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM