One - One Code All

Blog Content

Pandas中merge合并Dataframe报错:merge on datetime64[ns] and object columns

Python 统计学-科学计算   2015-02-09 21:59:40

ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat


这一般是合并的不同的dataframe中columns的数据类型(on="keys")不一致造成的。


检查办法:print (df.dtypes)


解决办法:类型转换。


1. 先存为csv, 再重新导入。


2. 类型转换:

df['keys'] = df['keys'].apply(int)

index.index = pd.to_datetime(index.index, format="%Y-%m-%d")

# convert the 'Date' column to datetime format 
df['Date']= pd.to_datetime(df['Date']) 
# Check the format of 'Date' column 
df.info() 

# convert the 'Date' column to datetime format 
df['Date'] = df['Date'].astype('datetime64[ns]') 
# Check the format of 'Date' column 
df.info()


Index.astype

Index.astype(dtype, copy=True)

参数

dtype : numpy dtype or pandas type

copy : bool, default True


其他转换方法

to_numeric() (conversion to numeric dtypes) 

to_datetime() (conversion to datetime objects) 

to_timedelta() (conversion to timedelta objects)



上一篇:python中matplotlib的颜色及线条控制
下一篇:pandas中dataframe和series索引定位和选择loc:Indexing and Selecting Data

The minute you think of giving up, think of the reason why you held on so long.