[英]Convert (back and forth) UNIX timestamp to pandas.tslib.Timestamp and datetime for series
I am working with python 3.5.2, pandas 0.18.1 and sqlite3.我正在使用 python 3.5.2、pandas 0.18.1 和 sqlite3。
In my data base, I have a column unix_time
with INT
for seconds since 1970. Ideally I want to read my dataframe from sqlite, and then create a time
column which would correspond to the datetime
or pandas.tslib.Timestamp
conversion of the unix_time
column that I woul only use for some processing and then drop before saving the dataframe back.在我的数据库中,我有一列unix_time
和INT
自 1970 年以来的秒数。理想情况下,我想从 sqlite 读取我的数据帧,然后创建一个time
列,它对应于unix_time
列的datetime
或pandas.tslib.Timestamp
转换我只会用于一些处理,然后在保存数据帧之前删除。
The issue is that when parsing the unix_time
column using :问题是在解析unix_time
列时使用:
df = pd.read_from_sql_query("SELECT * FROM test", con, parse_dates=['unix_time'])
I obtain pandas.tslib.Timestamp
types which is fine for my processing, but then I have to recreate my original unix_time
column using :我获得了适合我的处理的pandas.tslib.Timestamp
类型,但随后我必须使用以下方法重新创建我的原始unix_time
列:
df['unix_time'][i] = (df['unix_time'][i] - datetime(1970,1,1)).total_seconds()
which is really 'dirty'这真的很“脏”
First question : Do you have a better way?第一个问题:你有更好的方法吗?
I thought about giving up the unix time format and only use datetime
format but the to_datetime
method from pandas returns in fact pandas.tslib.Timestamp
... And anyway, doing so would force me to iterate over all rows which is a bad solution.我想放弃 unix 时间格式,只使用datetime
格式,但是 pandas 的to_datetime
方法实际上返回了pandas.tslib.Timestamp
......无论如何,这样做会迫使我遍历所有行,这是一个糟糕的解决方案。 (It is impossible to apply to_datetime
on something else than a view over a single cell of the dataframe (除了数据帧的单个单元格上的视图to_datetime
,不可能将to_datetime
应用于其他内容
Second question : Is it possible to apply it on a series?第二个问题:是否可以将其应用于系列?
My last try was with directly using df['time'] = datetime.datetime.fromtimestamp(df['unix_time'])
but surprisingly, it also returns pandas.tslib.Timestamp
.我的最后一次尝试是直接使用df['time'] = datetime.datetime.fromtimestamp(df['unix_time'])
但令人惊讶的是,它也返回pandas.tslib.Timestamp
。
In the end, knowing that I can only save unix timestamps or datetimes, my only choices for the moment are :最后,知道我只能保存 unix 时间戳或日期时间,我目前唯一的选择是:
parsing but then having to convert them back to unix timestamp one by one.解析但随后必须将它们一一转换回 unix 时间戳。
Or not parse it but have to convert them to pandas.tslib.Timestamp
one by one.或者不解析它而是必须将它们pandas.tslib.Timestamp
转换为pandas.tslib.Timestamp
。
It would be great if I could convert a whole series.如果我可以转换整个系列,那就太好了。
Last question : Is there a way to convert a unix timestamps series to datetime
(or at least pandas.tslib.Timestamp
), or a pandas.tslib.Timestamp
(or datetime
) series to unix timestamps?最后一个问题:有没有办法将 unix 时间戳系列转换为datetime
(或至少pandas.tslib.Timestamp
),或将pandas.tslib.Timestamp
(或datetime
)系列转换为 unix 时间戳?
Thanks谢谢
EDIT: During my processing, I extract a row that I want to append to my dataset.编辑:在处理过程中,我提取了要附加到数据集的行。 Apparently, the coversion to pandas.tslib.Timestamp
appends implicitly when passing from dataframe to serie :显然,当从数据帧传递到 serie 时,对 pandas.tslib.Timestamp 的pandas.tslib.Timestamp
隐式附加:
df = pd.DataFrame({'UNX':pd.date_range('2016-01-01', freq='9999S', periods=10).astype(np.int64)//10**9})
df['Date'] = pd.to_datetime(df.UNX, unit='s')
print(df.Date.dtypes)
print(type(df['Date'][0]))
test = df.iloc[0]
print(type(test.Date))
new_df = test.to_frame().transpose() #from here, impossible to do : new_df.to_sql("test", con) because the type for 'Date' is not supported
print(new_df.Date.dtypes)
returns返回
datetime64[ns]
<class 'pandas.tslib.Timestamp'>
<class 'pandas.tslib.Timestamp'>
object
Is there a way to convert the 'Date' in new_df
from pandas.tslib.Timestamp
to datetime64[ns]
or datetime.datetime
(or simply str
) ?有没有办法将new_df
的“日期”从pandas.tslib.Timestamp
为datetime64[ns]
或datetime.datetime
(或只是str
)?
IIUC you can do it this way: IIUC 你可以这样做:
In [96]: df = pd.DataFrame({'UNX':pd.date_range('2016-01-01', freq='9999S', periods=10).astype(np.int64)//10**9})
In [97]: df
Out[97]:
UNX
0 1451606400
1 1451616399
2 1451626398
3 1451636397
4 1451646396
5 1451656395
6 1451666394
7 1451676393
8 1451686392
9 1451696391
Convert UNIX epoch to Python datetime:将 UNIX 纪元转换为 Python 日期时间:
In [98]: df['Date'] = pd.to_datetime(df.UNX, unit='s')
In [99]: df
Out[99]:
UNX Date
0 1451606400 2016-01-01 00:00:00
1 1451616399 2016-01-01 02:46:39
2 1451626398 2016-01-01 05:33:18
3 1451636397 2016-01-01 08:19:57
4 1451646396 2016-01-01 11:06:36
5 1451656395 2016-01-01 13:53:15
6 1451666394 2016-01-01 16:39:54
7 1451676393 2016-01-01 19:26:33
8 1451686392 2016-01-01 22:13:12
9 1451696391 2016-01-02 00:59:51
Convert datetime
to UNIX epoch:将datetime
时间转换为 UNIX 纪元:
In [100]: df['UNX2'] = df.Date.astype('int64')//10**9
In [101]: df
Out[101]:
UNX Date UNX2
0 1451606400 2016-01-01 00:00:00 1451606400
1 1451616399 2016-01-01 02:46:39 1451616399
2 1451626398 2016-01-01 05:33:18 1451626398
3 1451636397 2016-01-01 08:19:57 1451636397
4 1451646396 2016-01-01 11:06:36 1451646396
5 1451656395 2016-01-01 13:53:15 1451656395
6 1451666394 2016-01-01 16:39:54 1451666394
7 1451676393 2016-01-01 19:26:33 1451676393
8 1451686392 2016-01-01 22:13:12 1451686392
9 1451696391 2016-01-02 00:59:51 1451696391
Check:检查:
In [102]: df.UNX.eq(df.UNX2).all()
Out[102]: True
Round trip between Pandas Timestamp and Unix Seconds (since 1970-01-01): Pandas Timestamp 和 Unix Seconds 之间的往返(自 1970-01-01):
date_in = pd.to_datetime("2022-04-07")
# type(date_in) is: pandas._libs.tslibs.timestamps.Timestamp
unix_seconds = date_in.value//10**9
date_out = pd.to_datetime(unix_seconds, unit="s")
Output:输出:
date_in
Out[1]: Timestamp('2021-04-07 00:00:00')
unix_seconds
Out[2]: 1617753600
date_out
Out[3]: Timestamp('2021-04-07 00:00:00')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.