简体   繁体   中英

pandas raises value error for datetime objects

my dataframe has a datetime index. I can not print its info as it raises a TypeError :

import pandas as pd
from datetime import datetime

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

results in

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

its worked on pandas 1.1.4 so kindly update your pandas package

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM