Python int-将datetime转换为Unix时间历元时出现字符串类型错误

Python int-将datetime转换为Unix时间历元时出现字符串类型错误,python,pandas,datetime,unix-timestamp,Python,Pandas,Datetime,Unix Timestamp,我正在尝试将Datetime转换为Unix时间纪元,但出现以下错误 输入: userid,datetime,latitude,longitude 156,2014-02-01 00:00:00.739166+01,41.8836718276551,12.4877775603346 187,2014-02-01 00:00:01.148457+01,41.9285433333333,12.4690366666667 297,2014-02-01 00:00:01.220066+01,41.8910

我正在尝试将Datetime转换为Unix时间纪元,但出现以下错误

输入:

userid,datetime,latitude,longitude
156,2014-02-01 00:00:00.739166+01,41.8836718276551,12.4877775603346
187,2014-02-01 00:00:01.148457+01,41.9285433333333,12.4690366666667
297,2014-02-01 00:00:01.220066+01,41.8910686119733,12.4927045625339
89,2014-02-01 00:00:01.470854+01,41.7931766914244,12.4321219603157
79,2014-02-01 00:00:01.631136+01,41.90027472,12.46274618
191,2014-02-01 00:00:02.048546+01,41.8523047579646,12.5774065771898
343,2014-02-01 00:00:02.647839+01,41.8921718255185,12.4696996165151
341,2014-02-01 00:00:02.709888+01,41.9102125627332,12.4770004336041
260,2014-02-01 00:00:03.458195+01,41.8658208551143,12.4655221109313
节目:

import pandas as pd
import numpy as np
import io

df = pd.read_csv('input.csv', 
                 #header=None, #no header in csv
                 header=['userid','datetime','latitude','longitude'], #set custom column names
                 parse_dates=['datetime']) #parse columns d, e to datetime

df['datetime'] = df['datetime'].astype(np.int64) // 10**9
#df['e'] = df['e'].astype(np.int64) // 10**9

df.to_csv('output.csv', header=True, index=False)
上面的程序在Python2.7中运行良好,但我没有升级到Python3.xAnaconda,因此无法获得结果

错误:

  File "pandas\parser.pyx", line 519, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:5907)

TypeError: Can't convert 'int' object to str implicitly

Edit:input file

中的
header
参数需要整数或整数列表,而不是字符串列表

from io import StringIO
file="""
userid,datetime,latitude,longitude
156,2014-02-01 00:00:00.739166+01,41.8836718276551,12.4877775603346
187,2014-02-01 00:00:01.148457+01,41.9285433333333,12.4690366666667
297,2014-02-01 00:00:01.220066+01,41.8910686119733,12.4927045625339
89,2014-02-01 00:00:01.470854+01,41.7931766914244,12.4321219603157
79,2014-02-01 00:00:01.631136+01,41.90027472,12.46274618
191,2014-02-01 00:00:02.048546+01,41.8523047579646,12.5774065771898
343,2014-02-01 00:00:02.647839+01,41.8921718255185,12.4696996165151
341,2014-02-01 00:00:02.709888+01,41.9102125627332,12.4770004336041
260,2014-02-01 00:00:03.458195+01,41.8658208551143,12.4655221109313"""
让我们试试这个read_csv语句:

df = pd.read_csv(StringIO(file),parse_dates=['datetime'])
df['datetime'] = df['datetime'].astype(np.int64) // 10**9

print(df.head())
输出:

   userid    datetime   latitude  longitude
0     156  1391209200  41.883672  12.487778
1     187  1391209201  41.928543  12.469037
2     297  1391209201  41.891069  12.492705
3      89  1391209201  41.793177  12.432122
4      79  1391209201  41.900275  12.462746

如果csv没有标题是必需的参数
names
parse_dates
[1]
-尝试将第二列解析为
datetime

import pandas as pd
import numpy as np
from pandas.compat import StringIO

temp=u"""156,2014-02-01 00:00:00.739166+01,41.8836718276551,12.4877775603346
187,1014-02-01 00:00:01.148457+01,41.9285433333333,12.4690366666667
297,2014-02-01 00:00:01.220066+01,41.8910686119733,12.4927045625339
89,2014-02-01 00:00:01.470854+01,41.7931766914244,12.4321219603157
79,2014-02-01 00:00:01.631136+01,41.90027472,12.46274618
191,2014-02-01 00:00:02.048546+01,41.8523047579646,12.5774065771898
343,2014-02-01 00:00:02.647839+01,41.8921718255185,12.4696996165151
341,2014-02-01 00:00:02.709888+01,41.9102125627332,12.4770004336041
260,2014-02-01 00:00:03.458195+01,41.8658208551143,12.4655221109313"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), 
                parse_dates=[1], 
                names=['userid','datetime','latitude','longitude'])
#print (df)

#check dtypes if datetime it is OK
print (df['datetime'].dtypes)
datetime64[ns] 
另一个可能的问题是错误的数据,在我的示例第二行中:

import pandas as pd
from pandas.compat import StringIO

temp=u"""156,2014-02-01 00:00:00.739166+01,41.8836718276551,12.4877775603346
187,1014-02-01 00:00:01.148457+01,41.9285433333333,12.4690366666667
297,2014-02-01 00:00:01.220066+01,41.8910686119733,12.4927045625339
89,2014-02-01 00:00:01.470854+01,41.7931766914244,12.4321219603157
79,2014-02-01 00:00:01.631136+01,41.90027472,12.46274618
191,2014-02-01 00:00:02.048546+01,41.8523047579646,12.5774065771898
343,2014-02-01 00:00:02.647839+01,41.8921718255185,12.4696996165151
341,2014-02-01 00:00:02.709888+01,41.9102125627332,12.4770004336041
260,2014-02-01 00:00:03.458195+01,41.8658208551143,12.4655221109313"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), 
                 parse_dates=[1], 
                 names=['userid','datetime','latitude','longitude'])

#print (df)

#check dtypes - parse failed, get object dtype
print (df['datetime'].dtypes)
object
使用和参数
errors='compresse'
解析到datetime-它将坏数据替换为
NaT
,然后将NaT替换为某个值,例如
0
1970-01-01 00:00:00.000000
)为:

编辑:


如果还有标题并且需要替换列名,则需要
header=0
添加到。

谢谢您的回答,但我收到以下错误:
ValueError:“datetime”不在列表中
csv文件的相同输入完全没有更改您可以将csv文件的前三行粘贴到此处吗?我已将输入文件作为链接提供给您可以从那里下载,应该可以。您正在执行相同的pd.read_cvs,我刚刚将StringIO(文件)更改为预期csv文件的正确路径?请确保使用括号表示列表的解析日期=['datetime']。您的程序中不需要StringIO,只需pd即可。read_csv('input.csv',parse_dates=['datetime'])非常感谢。。这太棒了!但我只能接受一个答案。你认为你能帮我解决这个问题吗:从2.x迁移到3.x感觉需要做很多改变。。是的,由您决定接受哪个答案。在您的第二个问题中,代码中的哪一行返回错误?这与基于Unix时间历元的数据操作差不多。
import pandas as pd
from pandas.compat import StringIO

temp=u"""156,2014-02-01 00:00:00.739166+01,41.8836718276551,12.4877775603346
187,1014-02-01 00:00:01.148457+01,41.9285433333333,12.4690366666667
297,2014-02-01 00:00:01.220066+01,41.8910686119733,12.4927045625339
89,2014-02-01 00:00:01.470854+01,41.7931766914244,12.4321219603157
79,2014-02-01 00:00:01.631136+01,41.90027472,12.46274618
191,2014-02-01 00:00:02.048546+01,41.8523047579646,12.5774065771898
343,2014-02-01 00:00:02.647839+01,41.8921718255185,12.4696996165151
341,2014-02-01 00:00:02.709888+01,41.9102125627332,12.4770004336041
260,2014-02-01 00:00:03.458195+01,41.8658208551143,12.4655221109313"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), 
                 parse_dates=[1], 
                 names=['userid','datetime','latitude','longitude'])

#print (df)

#check dtypes - parse failed, get object dtype
print (df['datetime'].dtypes)
object
df['datetime'] = pd.to_datetime(df['datetime'], errors='coerce').fillna(0)
print (df)
   userid                   datetime   latitude  longitude
0     156 2014-01-31 23:00:00.739166  41.883672  12.487778
1     187 1970-01-01 00:00:00.000000  41.928543  12.469037
2     297 2014-01-31 23:00:01.220066  41.891069  12.492705
3      89 2014-01-31 23:00:01.470854  41.793177  12.432122
4      79 2014-01-31 23:00:01.631136  41.900275  12.462746
5     191 2014-01-31 23:00:02.048546  41.852305  12.577407
6     343 2014-01-31 23:00:02.647839  41.892172  12.469700
7     341 2014-01-31 23:00:02.709888  41.910213  12.477000
8     260 2014-01-31 23:00:03.458195  41.865821  12.465522


df['datetime'] = df['datetime'].astype(np.int64) // 10**9
print (df)
   userid    datetime   latitude  longitude
0     156  1391209200  41.883672  12.487778
1     187           0  41.928543  12.469037
2     297  1391209201  41.891069  12.492705
3      89  1391209201  41.793177  12.432122
4      79  1391209201  41.900275  12.462746
5     191  1391209202  41.852305  12.577407
6     343  1391209202  41.892172  12.469700
7     341  1391209202  41.910213  12.477000
8     260  1391209203  41.865821  12.465522