Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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 从csv将数据转换为日期时间时出现问题。熊猫_Python_Datetime_Pandas - Fatal编程技术网

Python 从csv将数据转换为日期时间时出现问题。熊猫

Python 从csv将数据转换为日期时间时出现问题。熊猫,python,datetime,pandas,Python,Datetime,Pandas,我正在尝试将csv文件导入pandas数据框,将其中四列合并为“datetime”列,并将datetime列设置为数据类型Datetime64 这是我的原始数据的一个小样本: 0,1,2,3,4,5,6,7,8,9,10 0, 3001, 1, 1, 0, 0, 5, 4, 1.00, 0, 0, 0 1, 3001, 1, 1, 100, 0, 7, 5, 1.00, 0, 0, 0 2, 3001, 1, 1, 200, 0, 9, 6, 1.00, 0, 0, 0 3, 3

我正在尝试将csv文件导入pandas数据框,将其中四列合并为“datetime”列,并将datetime列设置为数据类型Datetime64

这是我的原始数据的一个小样本:

0,1,2,3,4,5,6,7,8,9,10
0, 3001, 1, 1,   0, 0,  5, 4, 1.00, 0, 0, 0 
1, 3001, 1, 1, 100, 0,  7, 5, 1.00, 0, 0, 0 
2, 3001, 1, 1, 200, 0,  9, 6, 1.00, 0, 0, 0 
3, 3001, 1, 1, 300, 0,  9, 7, 1.00, 0, 0, 0 
4, 3001, 1, 1, 400, 0, 11, 8, 1.00, 0, 0, 0
到目前为止,我已经:

dateparse = lambda x: datetime.strptime(x, '%Y %m %d %H%M')
Test = (read_csv(
   'file.csv',
   names=["year","month","day","hour","a","b","c","d","e","f","g"],
   parse_dates={"datetime": ["year","month","day","hour"]},
   date_parser=dateparse, 
   usecols=["year","month","day","hour","b"]
))
Test.head()
这似乎对许多其他人起了作用,但在这里不起作用。这就是产生的结果:

index, datetime, temp_hmean
0, 3001-01-01 00:00:00, 4.7 
1, 3001-01-01 01:00:00, 3.4 
2, 3001-01-01 02:00:00, 5.4 
3, 3001-01-01 03:00:00, 4.3 
4, 3001-01-01 04:00:00, 5.5 
我也尝试过添加“datetime”列到datetime之后的转换,正如其他文章所述,但它不起作用

此处是保留原始列的位置:

Test['datetime'] = Test.apply(
    lambda row: datetime.strptime(
        row['year']+ '-' + row['month']+ '-' + row['day']+ ' ' + row['hour'],
        '%Y-%m-%d %H%M'), 
    axis=1
)
或者在这里不保留原件:

Test['datetime'] = to_datetime(
    Test['datetime'], format="%Y-%m-%d %H:%M:%S"
)

您期望的输出是什么?确切地说是生成的数据帧,但datetime列应该在数据类型datetime64中,它作为对象显示出来。在
datetime
列中是否有任何NAN或NAT?转换为datetime64应该很简单。你试过这样的东西吗
dateparse=lambda x:numpy.datetime64(datetime.strtime(x,'%Y%m%d%H%m'))
任何列中都没有空值。您期望的输出是什么?确切地说是生成的数据帧,但datetime列应该是数据类型datetime64,在
datetime
列中是否有NAN或NAT?转换为datetime64应该很简单。你试过这样的东西吗
dateparse=lambda x:numpy.datetime64(datetime.strtime(x,'%Y%m%d%H%m'))
任何列中都没有空值。