Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从Float解析日期_Python_Date_Parsing_Datetime_Forecasting - Fatal编程技术网

在Python中从Float解析日期

在Python中从Float解析日期,python,date,parsing,datetime,forecasting,Python,Date,Parsing,Datetime,Forecasting,我有以下数据(实际上是来自机场的乘客) 如何将Python中的时间列解析为日期(TS)而不是浮点。在开始时间序列预测之前,我需要将此作为一个基本步骤 根据评论 时间以年为单位,是浮动的(1949.000是1949年1月,1949.0833是1949年2月) 我用它来导入数据,我不知道如何在read_csv中使用日期解析器 series = read_csv('http://vincentarelbundock.github.io/Rdatasets/csv/datasets/AirPasseng

我有以下数据(实际上是来自机场的乘客)

如何将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

看起来你的输入数据不是很精确。只是:

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 + 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

此外,正如Eric Duminil指出的,您的值似乎是四舍五入的。 如果这是真的,那么您可以这样做:

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
我想

1949.000  = 1st jan 1949

此外,正如Eric Duminil指出的,您的值似乎是四舍五入的。 如果这是真的,那么您可以这样做:

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

那是什么格式?1949.0应该代表什么时间?Unix时代结束后的1949秒?49年的第19天?午夜后19小时49分?我们无法知道。时间是用什么单位表示的?从链接上看,好像是1949年?你能举一个预期格式的例子吗?预期格式是TSA中的1-1-1949,那是什么格式?1949.0应该代表什么时间?Unix时代结束后的1949秒?49年的第19天?午夜后19小时49分?我们无法知道。时间是用什么单位表示的?从链接上看,好像是1949年?你能给出一个预期格式的例子吗?在TSI中预期格式为1-1-1949 get this TypeError:预期整数参数,预期浮点值,你使用哪个Python版本?我得到这个TypeError:预期整数参数,预期浮点值,你使用哪个Python版本?