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

Pandas: Using Unix epoch timestamp as Datetime index(Pandas:使用 Unix 纪元时间戳作为日期时间索引)
本文介绍了Pandas:使用 Unix 纪元时间戳作为日期时间索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

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

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

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:

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

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

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.

  1. 是否有处理此类时间戳格式的内置方法?
  2. 如果不是,推荐的处理这些格式的方法是什么?

推荐答案

将它们转换为 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])

这篇关于Pandas:使用 Unix 纪元时间戳作为日期时间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)