Pandas:使用 Unix 纪元时间戳作为日期时间索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16517240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:49:12  来源:igfitidea点击:

Pandas: Using Unix epoch timestamp as Datetime index

pythonnumpypandastime-series

提问by Nipun Batra

My application involves dealing with data (contained in a CSV) which is of the following form:

我的应用程序涉及处理以下形式的数据(包含在 CSV 中):

Epoch (number of seconds since Jan 1, 1970), Value
1368431149,20.3
1368431150,21.4
..

Currently i read the CSV using numpy loadtxt method (can easily use read_csv from Pandas). Currently for my series i am converting the timestamps field as follows:

目前我使用 numpy loadtxt 方法读取 CSV(可以轻松使用 Pandas 中的 read_csv)。目前,对于我的系列,我正在按如下方式转换时间戳字段:

timestamp_date=[datetime.datetime.fromtimestamp(timestamp_column[i]) for i in range(len(timestamp_column))]

I follow this by setting timestamp_date as the Datetime index for my DataFrame. I tried searching at several places to see if there is a quicker (inbuilt) way of using these Unix epoch timestamps, but could not find any. A lot of applications make use of such timestamp terminology.

我通过将 timestamp_date 设置为我的 DataFrame 的日期时间索引来遵循这一点。我尝试在几个地方搜索以查看是否有使用这些 Unix 纪元时间戳的更快(内置)方法,但找不到任何方法。许多应用程序使用这样的时间戳术语。

  1. Is there an inbuilt method for handling such timestamp formats?
  2. If not, what is the recommended way of handling these formats?
  1. 是否有处理此类时间戳格式的内置方法?
  2. 如果没有,处理这些格式的推荐方法是什么?

回答by eumiro

Convert them to datetime64[s]:

将它们转换为datetime64[s]

np.array([1368431149, 1368431150]).astype('datetime64[s]')
# array([2013-05-13 07:45:49, 2013-05-13 07:45:50], dtype=datetime64[s])

回答by queise

You can also use pandas to_datetime:

您还可以使用Pandasto_datetime

df['datetime'] = pd.to_datetime(df["timestamp"], unit='s')

This method requires Pandas 0.18 or later.

此方法需要 Pandas 0.18 或更高版本。

回答by Eric Blum

You can also use Pandas DatetimeIndex like so

您也可以像这样使用 Pandas DatetimeIndex

pd.DatetimeIndex(df['timestamp']*10**9)

the *10**9puts it into the format it's expecting for such timestamps.

*10**9把它转化成其期待这样的时间戳的格式。

This is nice since it allows you to use functions such as .date()or .tz_localize()on the series.

这很好,因为它允许您使用系列.date().tz_localize()系列之类的功能。