我正在遵循一个过程,在这个过程中,我必须将日期转换为unix时间戳。
import pandas as pd
df = pd.read_csv('file.csv')
print type(df.iloc[-1].name)类'pandas.tslib.Timestamp'
ts = df.iloc[-1].name.timestamp()AttributeError:“时间戳”对象没有属性“时间戳”
发布于 2016-06-21 14:08:42
实际上,您并不会问问题(下一次提示:要更明确),但我假设您希望从Pandas时间戳对象中获得一个划时代/ Unix时间戳。
如果使用pandas.tslib.Timestamp.value方法,您将以微秒(1/1,000,000,000秒)返回时间戳:
In [1]: import pandas as pd
In [2]: date_example = pd.to_datetime("2016-06-21")
In [3]: type(date_example)
Out[3]: pandas.tslib.Timestamp
In [4]: date_example.value
Out[4]: 1466467200000000000如果你愿意,你可以简单地除以1,000毫秒,或者1000000除以整秒,例如:
In [5]: date_example.value / 1000000
Out[5]: 1466467200000发布于 2016-06-21 14:30:19
IIUC,你可以这样做
用datetime dtype生成样例DF
In [65]: x = pd.DataFrame({'Date': pd.date_range('2016-01-01', freq='5D', periods=5)})
In [66]: x
Out[66]:
Date
0 2016-01-01
1 2016-01-06
2 2016-01-11
3 2016-01-16
4 2016-01-21将日期时间转换为UNIX时间戳
In [67]: x.Date.astype(np.int64) // 10**9
Out[67]:
0 1451606400
1 1452038400
2 1452470400
3 1452902400
4 1453334400
Name: Date, dtype: int64https://stackoverflow.com/questions/37945430
复制相似问题