python - Convert unix time to readable date in pandas DataFrame -


i have data frame unix times , prices in it. want convert index column shows in human readable dates. instance have "date" 1349633705 in index column i'd want show 10/07/2012 (or @ least 10/07/2012 18:15). context, here code i'm working , i've tried already:

import json import urllib2 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    df 

as can see i'm using df.date = df.date.apply(lambda d: datetime.strptime(d, "%y-%m-%d")) here doesn't work since i'm working integers, not strings. think need use datetime.date.fromtimestamp i'm not quite sure how apply whole of df.date. thanks.

these appear 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 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 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -