[英]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. 我认为df.to_dict()
会很简单,但无济于事。
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
. 他们会得到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()
. 我已经试过进口各种各样的事情,甚至试图玩弄不同orients
的pd.to_dict()
How do I either convert the Timestamps
or better yet, create a DataFrame from them? 如何转换Timestamps
或更好,从它们创建一个DataFrame?
You need to import Timestamp
from pandas
: 您需要从pandas
导入Timestamp
:
>>> 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
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
import module
doesn't enter the module's names into the global namespace, you have to access them via module.name
. import module
不会将模块的名称输入全局命名空间,您必须通过module.name
访问它们。 To enter the module's names into the global namespace, you need to use the from module import
syntax. 要将模块的名称输入全局命名空间,您需要使用from module import
语法。 In this case, either from pandas import Timestamps
, which enters Timestamps
into the global namespace, or from pandas import *
, which imports all of the names in pandas into the global namespace. 在这种情况下,要么是from pandas import Timestamps
,它将Timestamps
输入到全局命名空间,要么是from pandas import *
,它将from pandas import *
所有名称导入全局命名空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.