使用 pandas to_datetime与时间戳 [英] Using pandas to_datetime with timestamps

查看:598
本文介绍了使用 pandas to_datetime与时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把这些时间戳变成一个%Y-%m-%d%H:%M 格式。以下是数据示例:

  0 1450753200 
1 1450756800
2 1450760400
3 1450764000
4 1450767600
名称:ohlcv_start_date,dtype:int64

有人可以解释什么类型的时间戳这些是什么代码,我需要正确转换它们,因为当我使用:

  pd.to_datetime(df [TS],unit ='ms')。dt.strftime('%Y-%m-%d%H:%M')

将时间转换为:

  0 1970-01-01 00:00 
1 1970-01-01 00:00
2 1970-01-01 00:00
3 1970-01-01 00:00
4 1970-01-01 00: 00

哪个不正确



编辑:谢谢Chum先生。



我实际上要做的是将不同资产的值合并为时间戳。每个资产的启动和完成时间稍微不同,经过分析,数据似乎有差距:

  market_trading_pair next_future_timestep_return ohlcv_start_date \ 
0 Poloniex_ETH_BTC 3.013303e-03 2015-12-22 03
1 Poloniex_ETH_BTC 3.171481e-03 2015-12-22 05
2 Poloniex_ETH_BTC -1.381575e-03 2015-12-22 07
3 Poloniex_ETH_BTC -4.327704e-03 2015-12-22 08

最好的我可以想解决这个问题是创建一个新的数据框架,并用时间戳填写行数一个小时,从这里我可以简单的合并资产数据。任何想法如何产生升序时间戳?

解决方案

通过 unit ='s'获取值时代时代

 在[106]中:
pd.to_datetime(df ['timestamp'],unit ='s')
Out [106]:
index
0 2015-12-22 03:00:00
1 2015-12-22 04:00:00
2 2015-12-22 05:00:00
3 2015-12- 22 06:00:00
4 2015-12-22 07:00:00
名称:时间戳,dtype:datetime64 [ns]

如果您愿意,您可以转换为字符串:

 在[107 ]:

pd.to_datetime(df ['timestamp'],unit ='s')dt.strftime('%Y-%m-%d%H:%M')
Out [107]:
index
0 2015-12-22 03:00
1 2015-12-22 04:00
2 2015-12-22 05: 00
3 2015-12-22 06:00
4 2015-12-22 07:00
名称:时间戳,dtype:对象
/ pre>

I'm trying to covert my these timestamps into a %Y-%m-%d %H:%M format. Here's a sample of the data:

0    1450753200
1    1450756800
2    1450760400
3    1450764000
4    1450767600
Name: ohlcv_start_date, dtype: int64

Could someone explain what type of timestamp these are and what code I need to convert them properly, because when I use:

pd.to_datetime(df[TS], unit='ms').dt.strftime('%Y-%m-%d %H:%M')

It converts the time into:

0        1970-01-01 00:00
1        1970-01-01 00:00
2        1970-01-01 00:00
3        1970-01-01 00:00
4        1970-01-01 00:00

Which isn't correct

EDIT: Thanks Mr Chum.

What i'm actually trying to do is merge the values of different assets by timestamp. Each asset starts and finishes at slightly different times and Upon analysis it seems there is gaps in the data:

 market_trading_pair  next_future_timestep_return ohlcv_start_date  \
0        Poloniex_ETH_BTC                 3.013303e-03    2015-12-22 03      
1        Poloniex_ETH_BTC                 3.171481e-03    2015-12-22 05   
2        Poloniex_ETH_BTC                -1.381575e-03    2015-12-22 07   
3        Poloniex_ETH_BTC                -4.327704e-03    2015-12-22 08   

The best I can think to solve this problem is to create a new data frame and fill in the rows with time stamps incrementing by one hours, from here i can simple merge in the asset data. Any idea how to generate ascending timstamps ?

解决方案

Pass unit='s' to get the values as it's epoch time:

In [106]:
pd.to_datetime(df['timestamp'], unit='s')
Out[106]:
index
0   2015-12-22 03:00:00
1   2015-12-22 04:00:00
2   2015-12-22 05:00:00
3   2015-12-22 06:00:00
4   2015-12-22 07:00:00
Name: timestamp, dtype: datetime64[ns]

You can convert to string if you desire:

In [107]:

pd.to_datetime(df['timestamp'], unit='s').dt.strftime('%Y-%m-%d %H:%M')
Out[107]:
index
0    2015-12-22 03:00
1    2015-12-22 04:00
2    2015-12-22 05:00
3    2015-12-22 06:00
4    2015-12-22 07:00
Name: timestamp, dtype: object

这篇关于使用 pandas to_datetime与时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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