python - pandas 引发日期时间对象的值错误

标签 python pandas numpy

我的数据框有一个日期时间索引。我无法打印其信息,因为它引发了 TypeError:

import pandas as pd
from datetime import datetime

df = pd.DataFrame(data={"a": datetime.now()}, index=["a"])
print(df.info())

结果

Traceback (most recent call last):
  File "example.py", line 88, in <module>
    print(df.info())
  File "lib/python3.8/site-packages/pandas/core/frame.py", line 2497, in info
    mem_usage = self.memory_usage(index=True, deep=deep).sum()
  File "lib/python3.8/site-packages/pandas/core/frame.py", line 2590, in memory_usage
    result = Series(self.index.memory_usage(deep=deep), index=["Index"]).append(
  File "lib/python3.8/site-packages/pandas/core/series.py", line 305, in __init__
    data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
  File "lib/python3.8/site-packages/pandas/core/construction.py", line 465, in sanitize_array
    subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
  File "lib/python3.8/site-packages/pandas/core/dtypes/cast.py", line 1452, in construct_1d_arraylike_from_scalar
    subarr = np.empty(length, dtype=dtype)
TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type

最佳答案

它适用于 pandas 1.1.4,因此请更新您的 pandas

output of your code:-

<class 'pandas.core.frame.DataFrame'>
Index: 1 entries, a to a
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   a       1 non-null      datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 16.0+ bytes
None

关于python - pandas 引发日期时间对象的值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66280521/

相关文章:

python - 比较两个 XML 文件并更新其中一个文件中的元素

python - python 中的上下文管理器和辅助函数

python - 访问 bool numpy 矩阵的所有非对角线元素

python - 计算Python pandas中的系列数

python - 用 "\r\n"替换 "\n"

python - 有没有办法用gym的OpenAI来减慢游戏环境的速度?

python - 使用 groupby 时,如何在 pandas 中使用一个函数聚合多个列?

python - 如何将多个数据帧列合并为一个给定的每列都有 nan 值

python - 在 Python Pandas 中查找前导零的记录

python - 如何在 Pandas 中创建日期时间索引