pandas 从日期时间转换为整数时间戳 [英] pandas convert from datetime to integer timestamp

查看:191
本文介绍了 pandas 从日期时间转换为整数时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到python中的pandas数据帧具有名为整数类型的time列,我可以使用以下指令将其转换为datetime格式.

Considering a pandas dataframe in python having a column named time of type integer, I can convert it to a datetime format with the following instruction.

df['time'] = pandas.to_datetime(df['time'], unit='s')

因此,该列现在具有类似2019-01-15 13:25:43的条目.

so now the column has entries like: 2019-01-15 13:25:43.

将字符串恢复为整数时间戳值(代表从1970-01-01 00:00:00开始经过的秒数)的命令是什么?

What is the command to revert the string to an integer timestamp value (representing the number of seconds elapsed from 1970-01-01 00:00:00)?

我检查了pandas.Timestamp,但找不到转换实用程序,因此无法使用pandas.to_timedelta.

I checked pandas.Timestamp but could not find a conversion utility and I was not able to use pandas.to_timedelta for this.

此转换有实用程序吗?

推荐答案

您可以使用astype(int)将广播类型转换为int,然后将其除以10**9,以获取unix纪元开始的秒数.

You can typecast to int using astype(int) and divide it by 10**9 to get the number of seconds to the unix epoch start.

import pandas as pd
df = pd.DataFrame({'time': [pd.to_datetime('2019-01-15 13:25:43')]})
df_unix_sec = pd.to_datetime(df['time']).astype(int)/ 10**9
print(df_unix_sec)

这篇关于 pandas 从日期时间转换为整数时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆