python - 为 Pandas 系列的 float 分配日期

标签 python pandas datetime series

我有一个包含一些 floatsnan 的系列,我正在尝试覆盖一些丢失的数据。

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/

相关文章:

python - 未加权双向图上的广度优先搜索

python - python统计txt文件中某个单词的出现次数

python - 使用 np.nans 从 python 数据框中选择数据部分

pandas - 重写数据框

python - 如何找到一个点位于 GeoPandas DataFrame 的哪个多边形内?

c# - 如何让天数在范围内

python - 什么信号可以连接到pyqt4(qt)中的初始对话框显示

python - 在 python 上使用正则表达式进行复杂的数据清理

VB.NET 没有日期时间?数据读取器

ruby-on-rails - 对于MySQL和Sqlite3,迁移文件中Rails的日期时间和时间戳是否相同?