This error occurs when you try to convert a datetime object to an int data type using the astype() method, which is not supported.

For example, the following code will raise this error:

import pandas as pd
df = pd.DataFrame({'date': ['2022-01-01']})
df['date'] = df['date'].astype(int)

To fix this error, you should convert the datetime object to a compatible data type, such as a string or a timestamp, before converting it to an int. For example, you can use the pandas to_datetime() method to convert the date column to a datetime object, and then use the timestamp() method to convert it to an int:

import pandas as pd
df = pd.DataFrame({'date': ['2022-01-01']})
df['date'] = pd.to_datetime(df['date']).astype(int) // This line converts datetime to int
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]

原文地址: https://www.cveoy.top/t/topic/txS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录