Python 将unix时间转换为pandas数据帧中的可读日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19231871/
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
Convert unix time to readable date in pandas dataframe
提问by W A Carnegie
I have a dataframe with unix times and prices in it. I want to convert the index column so that it shows in human readable dates.
我有一个包含 unix 时间和价格的数据框。我想转换索引列,以便它以人类可读的日期显示。
So for instance I have date
as 1349633705
in the index column but I'd want it to show as 10/07/2012
(or at least 10/07/2012 18:15
).
因此,例如我在索引列中有date
as 1349633705
,但我希望它显示为10/07/2012
(或至少为10/07/2012 18:15
)。
For some context, here is the code I'm working with and what I've tried already:
对于某些情况,这是我正在使用的代码以及我已经尝试过的代码:
import json
import urllib2
from datetime import datetime
response = urllib2.urlopen('http://blockchain.info/charts/market-price?&format=json')
data = json.load(response)
df = DataFrame(data['values'])
df.columns = ["date","price"]
#convert dates
df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
df.index = df.date
As you can see I'm using
df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
here which doesn't work since I'm working with integers, not strings. I think I need to use datetime.date.fromtimestamp
but I'm not quite sure how to apply this to the whole of df.date
.
正如您所看到的,我在df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
这里使用
它不起作用,因为我使用的是整数,而不是字符串。我想我需要使用,datetime.date.fromtimestamp
但我不太确定如何将它应用于整个df.date
.
Thanks.
谢谢。
采纳答案by Jeff
These appear to be seconds since epoch.
这些似乎是自纪元以来的几秒钟。
In [20]: df = DataFrame(data['values'])
In [21]: df.columns = ["date","price"]
In [22]: df
Out[22]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 358 entries, 0 to 357
Data columns (total 2 columns):
date 358 non-null values
price 358 non-null values
dtypes: float64(1), int64(1)
In [23]: df.head()
Out[23]:
date price
0 1349720105 12.08
1 1349806505 12.35
2 1349892905 12.15
3 1349979305 12.19
4 1350065705 12.15
In [25]: df['date'] = pd.to_datetime(df['date'],unit='s')
In [26]: df.head()
Out[26]:
date price
0 2012-10-08 18:15:05 12.08
1 2012-10-09 18:15:05 12.35
2 2012-10-10 18:15:05 12.15
3 2012-10-11 18:15:05 12.19
4 2012-10-12 18:15:05 12.15
In [27]: df.dtypes
Out[27]:
date datetime64[ns]
price float64
dtype: object
回答by Sandesh
If you try using:
如果您尝试使用:
df[DATE_FIELD]=(pd.to_datetime(df[DATE_FIELD],***unit='s'***))
and receive an error :
并收到一个错误:
"pandas.tslib.OutOfBoundsDatetime: cannot convert input with unit 's'"
“pandas.tslib.OutOfBoundsDatetime:无法使用单位's'转换输入”
This means the DATE_FIELD
is not specified in seconds.
这意味着DATE_FIELD
不是以秒为单位指定的。
In my case, it was milli seconds - EPOCH time
.
就我而言,它是毫秒 - EPOCH time
。
The conversion worked using below:
转换工作使用以下:
df[DATE_FIELD]=(pd.to_datetime(df[DATE_FIELD],unit='ms'))
回答by fahim reza
Assuming we imported pandas as pd
and df
is our dataframe
假设我们导入了pandas as pd
并且df
是我们的数据框
pd.to_datetime(df['date'], unit='s')
works for me.
为我工作。
回答by bakka
Alternatively, by changing a line of the above code:
或者,通过更改上述代码的一行:
# df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
df.date = df.date.apply(lambda d: datetime.datetime.fromtimestamp(int(d)).strftime('%Y-%m-%d'))
It should also work.
它也应该工作。