【问题标题】:Sanitizing Time Series whose plots shows erratic graph lines清理绘图显示不稳定图形线的时间序列
【发布时间】:2020-02-19 14:26:00
【问题描述】:

我想绘制时间线,我的日期格式为日/月/年。

在构建索引时,我会处理:

# format Date
test['DATA'] = pd.to_datetime(test['DATA'], format='%d/%m/%Y')
test.set_index('DATA', inplace=True)

通过仔细检查,我发现月份和日期的解释正确:

#the number of month reflect the month, not the day : correctly imported!
test['Year'] = test.index.year
test['Month'] = test.index.month   
test['Weekday Name'] = test.index.weekday_name

但是,当我绘图时,我看到数据点连接不规律(尽管它们的分布似乎是正确的,因为我预计会有季节性):

# Start and end of the date range to extract
start, end = '2018-01', '2018-04'

# Plot daily, weekly resampled, and 7-day rolling mean time series together

fig, ax = plt.subplots()
ax.plot(test.loc['2018', 'TMIN °C'],
marker='.', linestyle='-', linewidth=0.5, label='Daily')

我怀疑这可能与错误解释的日期有关,或者日期没有按正确的顺序排列,但无法找到验证错误所在的方法。

您能否帮助验证如何正确导入我的时间序列?

【问题讨论】:

标签: python pandas datetime time-series seaborn


【解决方案1】:

哦,这非常简单。我假设 datetime 是自动排序的,而不是必须排序:

test.loc['2018-01':'2018-03'].sort_index().index #sorted
test.loc['2018-01':'2018-03'].index #not sorted

这个问题可能会被延迟或标记为重复,我让版主:

Pandas - Sorting a dataframe by using datetimeindex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2016-06-24
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多