请问为什么改不了Dtype类型?


print("############################################")
print("          Info Of the Data Set")
print("############################################")
overall.info()

0 post_date 567 non-null object
1 count_id_indexed 567 non-null float64
dtypes: float64(1), object(1)
memory usage: 9.0+ KB

通过下面的代码,post_date的Dtype还是没有改变


overall.post_date  = overall.post_date .astype(str)
print("######################################################################")
print("          After Cleaning and type covertion in the Data Set")
print("######################################################################")
overall.info()

0 post_date 567 non-null object
1 count_id_indexed 567 non-null float64
dtypes: float64(1), object(1)
memory usage: 9.0+ KB

字符串在Pandas中的类型为object,由于原数据中要转换的列是字符串或不规整数据,转换为字符串类型时仍为object,参考这里的解释:
https://zhuanlan.zhihu.com/p/161073173
用如下代码你就可以看到转换效果:

import pandas as pd

df=pd.DataFrame({'post_date':['2012-02-02','2012-02-03'],'value':[1098.28,2087.85]})
df.post_date=pd.to_datetime(df.post_date)
print(df.info())
df.post_date=df.post_date.astype(str)
print(df.info())

overall.post_date = overall.post_date .astype(str)
这是把post_date 赋值成了post_date .astype(str)这个东西,不是修改它的类型