从 Python 中的 Float 解析日期
Parsing Dates from Float in Python
我有以下数据(实际上是 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) 而不是浮点数。在开始时间序列预测之前,我需要将其作为基本步骤
基于评论
时间以年为单位,是一个浮点数(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 + float(month)/12
您可以遍历您的行号:
import datetime
start_year = 1949
for line_number in range(20):
print datetime.date(start_year + line_number/12, line_number % 12 + 1 , 1)
它输出:
1949-01-01
1949-02-01
1949-03-01
1949-04-01
1949-05-01
1949-06-01
1949-07-01
1949-08-01
1949-09-01
1949-10-01
1949-11-01
1949-12-01
1950-01-01
1950-02-01
1950-03-01
1950-04-01
1950-05-01
1950-06-01
1950-07-01
1950-08-01
如果你真的想解析字符串,你可以试试:
import datetime
year_str = "1949.166667"
year_float = float(year_str)
year = int(year_float)
year_start = datetime.date(year,1,1)
delta = datetime.timedelta(days = int((year_float-year)*365) )
print year_start + delta
# 1949-03-02
这样,数据点之间的步长将正好是一年的 1/12。
我想,
1949.000 = 1st jan 1949
和
1949.9999... = 31th dec 1949
此外,正如 Eric Duminil 所指出的,您的价值观似乎是 month-rounded。
如果是这样,那么您可以这样做:
import datetime
from dateutil.relativedelta import relativedelta
def floatToDate(date_as_float):
year = int(date_as_float)
months_offset = round((date_as_float - float(year)) * 12.0, 0)
new_date = datetime.datetime(year,01,01,0,0,0,0)
new_date = new_date + relativedelta(months=int(months_offset))
return new_date
converted = floatToDate(1949.083333) # datetime 01-feb-1949
我有以下数据(实际上是 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) 而不是浮点数。在开始时间序列预测之前,我需要将其作为基本步骤
基于评论 时间以年为单位,是一个浮点数(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 + float(month)/12
您可以遍历您的行号:
import datetime
start_year = 1949
for line_number in range(20):
print datetime.date(start_year + line_number/12, line_number % 12 + 1 , 1)
它输出:
1949-01-01
1949-02-01
1949-03-01
1949-04-01
1949-05-01
1949-06-01
1949-07-01
1949-08-01
1949-09-01
1949-10-01
1949-11-01
1949-12-01
1950-01-01
1950-02-01
1950-03-01
1950-04-01
1950-05-01
1950-06-01
1950-07-01
1950-08-01
如果你真的想解析字符串,你可以试试:
import datetime
year_str = "1949.166667"
year_float = float(year_str)
year = int(year_float)
year_start = datetime.date(year,1,1)
delta = datetime.timedelta(days = int((year_float-year)*365) )
print year_start + delta
# 1949-03-02
这样,数据点之间的步长将正好是一年的 1/12。
我想,
1949.000 = 1st jan 1949
和
1949.9999... = 31th dec 1949
此外,正如 Eric Duminil 所指出的,您的价值观似乎是 month-rounded。 如果是这样,那么您可以这样做:
import datetime
from dateutil.relativedelta import relativedelta
def floatToDate(date_as_float):
year = int(date_as_float)
months_offset = round((date_as_float - float(year)) * 12.0, 0)
new_date = datetime.datetime(year,01,01,0,0,0,0)
new_date = new_date + relativedelta(months=int(months_offset))
return new_date
converted = floatToDate(1949.083333) # datetime 01-feb-1949