【发布时间】:2017-02-14 14:10:13
【问题描述】:
我有以下数据(实际上是来自http://vincentarelbundock.github.io/Rdatasets/datasets.html 的 AirPassengers)
time AirPassengers
1 1949.000000 112
2 1949.083333 118
3 1949.166667 132
4 1949.250000 129
5 1949.333333 121
6 1949.416667 135
如何将 Python 中的时间列解析为日期 (TS) 而不是浮点数。在开始时间序列预测之前,我需要这是一个基本步骤
基于 cmets 时间以年为单位,是一个浮点数(1949.000 是 1949 年 1 月,1949.0833 是 1949 年 2 月)
我正在使用它来导入数据,我不知道如何在 read_csv 中使用日期解析器
series = read_csv('http://vincentarelbundock.github.io/Rdatasets/csv/datasets/AirPassengers.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, )
更新-
一种可能的解决方案 - 忽略浮点值并使用开始、结束和时间间隔创建日期时间序列
series['dates']=pd.date_range('1949-01', '1961-01', freq='M')
series.head()
time AirPassengers dates
1 1949.000000 112 1949-01-31
2 1949.083333 118 1949-02-28
3 1949.166667 132 1949-03-31
4 1949.250000 129 1949-04-30
5 1949.333333 121 1949-05-31
In [45]:
series.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 144 entries, 1 to 144
Data columns (total 3 columns):
time 144 non-null float64
AirPassengers 144 non-null int64
dates 144 non-null datetime64[ns]
dtypes: datetime64[ns](1), float64(1), int64(1)
memory usage: 4.5 KB
请注意新问题 - 显示月份的结束日期(不是开始),我们将浮点值转换为日期时间值的原始问题仍然存在
Python 版本
!pip install version_information
%load_ext version_information
%version_information
Software Version
Python 3.5.2 64bit [MSC v.1900 64 bit (AMD64)]
IPython 5.1.0
OS Windows 7 6.1.7600 SP0
【问题讨论】:
-
那是什么格式?
-
1949.0应该代表什么时间? Unix 纪元后 1,949 秒? '49 年的第 19 天?午夜后 19 小时 49 分钟?我们无从得知。
-
时间用什么单位表示?从链接来看,它看起来像是 1949 年?你能举一个预期格式的例子吗?
-
TS 中的预期格式为 1-1-1949
标签: python date parsing datetime forecasting