将字符串转换为
datetimeusing
to_datetime,然后减去datetime
1970-1-1并调用
dt.total_seconds():
In [2]:import pandas as pdimport datetime as dtdf = pd.Dataframe({'date':['2011-04-24 01:30:00.000']})dfOut[2]:date0 2011-04-24 01:30:00.000In [3]:df['date'] = pd.to_datetime(df['date'])dfOut[3]: date0 2011-04-24 01:30:00In [6]: (df['date'] - dt.datetime(1970,1,1)).dt.total_seconds()Out[6]:0 1303608600Name: date, dtype: float64
您可以看到,将此值转换回产生的时间相同:
In [8]:pd.to_datetime(1303608600, unit='s')Out[8]:Timestamp('2011-04-24 01:30:00')
因此,您可以添加新列或覆盖:
In [9]:df['epoch'] = (df['date'] - dt.datetime(1970,1,1)).dt.total_seconds()dfOut[9]: date epoch0 2011-04-24 01:30:00 1303608600
编辑
@Jeff建议的更好的方法:
In [3]:df['date'].astype('int64')//1e9Out[3]:0 1303608600Name: date, dtype: float64In [4]:%timeit (df['date'] - dt.datetime(1970,1,1)).dt.total_seconds()%timeit df['date'].astype('int64')//1e9100 loops, best of 3: 1.72 ms per loop1000 loops, best of 3: 275 µs per loop
您还可以看到它明显更快
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)