我有一个包含一些 floats
和 nan
的系列,我正在尝试覆盖一些丢失的数据。
from pandas.core.series import Series
from datetime import datetime
d = {'name': None, 'purchase_date': None, 'value': 195146.0}
s = Series(d)
print(s['purchase_date'])
s['purchase_date'] = datetime(2019,1,3)
请注意下面最后一行的奇怪错误,即使我能够在 print
语句中访问 'purchase_date'
。
ValueError: ['p' 'u' 'r' 'c' 'h' 'a' 's' 'e' '_' 'd' 'a' 't' 'e'] not contained in the index
.
很有趣,用数字替换 datetime
行效果很好
s['purchase_date'] = 21
最佳答案
这是一个将在下一个 pandas 版本 (0.24.0) 中修复的错误:
In [1]: from datetime import datetime
...: import pandas as pd
In [2]: pd.__version__
Out[2]: '0.24.0.dev0+1469.g0b45abbe2'
In [3]: d = {'name': None, 'purchase_date': None, 'value': 195146.0}
In [4]: s = pd.Series(d)
In [5]: s['purchase_date'] = datetime(2019,1,3)
In [6]: s
Out[6]:
name NaN
purchase_date 2019-01-03 00:00:00
value 195146
dtype: object
对应的 GitHub issue 见:https://github.com/pandas-dev/pandas/issues/23451
关于python - 为 Pandas 系列的 float 分配日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54027831/