Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas to_dict() Returning "Timestamp"

Well this is embarrassing... I'm trying to create a good reproducible pandas example by giving you guys a small sample of my dataset. I thought this would be simple with df.to_dict() but to no avail.

df2 = df1[['DATE_FILLED','DAYS_SUPPLY']].head(5)
df2['DATE_FILLED'] = pd.to_datetime(df2['DATE_FILLED'])
diction = df2.to_dict()

output:

{'DATE_FILLED': {0: Timestamp('2016-12-28 00:00:00'),
                 1: Timestamp('2016-12-31 00:00:00'), 
                 2: Timestamp('2016-12-20 00:00:00'), 
                 3: Timestamp('2016-12-21 00:00:00'), 
                 4: Timestamp('2016-12-26 00:00:00')}, 
     'DAYS_SUPPLY': {0: 14, 1: 14, 2: 14, 3: 7, 4: 7}}

But if the community were to convert it to a dataframe by using the text:

import pandas as pd
from datetime import datetime
import time
d= pd.DataFrame({'DATE_FILLED': [Timestamp('2016-12-28 00:00:00'), Timestamp('2016-12-31 00:00:00'), Timestamp('2016-12-20 00:00:00'), Timestamp('2016-12-21 00:00:00'), Timestamp('2016-12-26 00:00:00')], 'DAYS_SUPPLY': [14, 14, 14, 7, 7]})

They would get NameError: name 'Timestamp' is not defined. I've tried importing various things and even tried playing around with the different orients in pd.to_dict().

How do I either convert the Timestamps or better yet, create a DataFrame from them?

like image 698
MattR Avatar asked Mar 16 '17 20:03

MattR


Video Answer


2 Answers

You need to import Timestamp from pandas:

>>> import pandas as pd
>>> from pandas import Timestamp
>>> d= pd.DataFrame({'DATE_FILLED': [Timestamp('2016-12-28 00:00:00'), Timestamp('2016-12-31 00:00:00'), Timestamp('2016-12-20 00:00:00'), Timestamp('2016-12-21 00:00:00'), Timestamp('2016-12-26 00:00:00')], 'DAYS_SUPPLY': [14, 14, 14, 7, 7]})
>>>
>>> d
  DATE_FILLED  DAYS_SUPPLY
0  2016-12-28           14
1  2016-12-31           14
2  2016-12-20           14
3  2016-12-21            7
4  2016-12-26            7
>>>

In the future, you can always use introspection to give you a good hint:

>>> ts = d.to_dict()['DATE_FILLED'][0]
>>> type(ts)
<class 'pandas.tslib.Timestamp'>
>>> from pandas.tslib import Timestamp
like image 150
juanpa.arrivillaga Avatar answered Sep 20 '22 15:09

juanpa.arrivillaga


You just need to import Timestamp:

from pandas import Timestamp

d = {'DATE_FILLED': {0: Timestamp('2016-12-28 00:00:00'),
                 1: Timestamp('2016-12-31 00:00:00'), 
                 2: Timestamp('2016-12-20 00:00:00'), 
                 3: Timestamp('2016-12-21 00:00:00'), 
                 4: Timestamp('2016-12-26 00:00:00')}, 
     'DAYS_SUPPLY': {0: 14, 1: 14, 2: 14, 3: 7, 4: 7}}



pd.DataFrame(d)
Out: 
  DATE_FILLED  DAYS_SUPPLY
0  2016-12-28           14
1  2016-12-31           14
2  2016-12-20           14
3  2016-12-21            7
4  2016-12-26            7
like image 38
ayhan Avatar answered Sep 19 '22 15:09

ayhan