我的数据框有一个日期时间索引。我无法打印其信息,因为它引发了 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/