Python 熊猫数据类型不';t返回数据类型字符串或日期

Python 熊猫数据类型不';t返回数据类型字符串或日期,python,pandas,dtype,Python,Pandas,Dtype,我使用pandas读入的CSV文件作为数据帧,我希望有一个每列所有数据类型的列表作为输出-这就是我到目前为止所拥有的-我遇到的问题是,对于所有不是浮点数/整数的数据类型,它只返回dtype('O') 我的代码如下所示: dataframe = pd.read_csv(filePath) datatypes = dataframe.dtypes #here we find out how what the datatype is in a given column datatypes_list

我使用pandas读入的CSV文件作为数据帧,我希望有一个每列所有数据类型的列表作为输出-这就是我到目前为止所拥有的-我遇到的问题是,对于所有不是浮点数/整数的数据类型,它只返回dtype('O')

我的代码如下所示:

dataframe = pd.read_csv(filePath)
datatypes = dataframe.dtypes   #here we find out how what the datatype is in a given column
datatypes_list = []
for x in datatypes:
    datatypes_list.append(x)
    
datatypes_list
[dtype('string'), dtype('int64'), dtype('float64'), dtype('datetime')]
这给了我,对于下面的csv

Position,Experience in Years,Salary,Starting Date
Middle Management,5,5584.10, 2019-02-03
Lower Management,2,3925.52,2016-04-18
Upper Management,1,7174.46,2019-01-02
Middle Management,5,5461.25,2018-02-02
Middle Management,7,7471.43,2017-09-09
Upper Management,10,12021.31,2020-01-01
Lower Management,2,2921.92,2019-08-17
Middle Management,5,5932.94,2017-11-21
Upper Management,7,10192.14,2018-08-18
此输出:

[dtype('O'), dtype('int64'), dtype('float64'), dtype('O')]
但我希望输出是这样的:

dataframe = pd.read_csv(filePath)
datatypes = dataframe.dtypes   #here we find out how what the datatype is in a given column
datatypes_list = []
for x in datatypes:
    datatypes_list.append(x)
    
datatypes_list
[dtype('string'), dtype('int64'), dtype('float64'), dtype('datetime')]

并且仅返回dtype('O')如果您的dtype数组的相应列中有不同的数据类型,则第一个位置可以解释为字符串dtype,因为在pandas中,“O”代表对象类型,这意味着pandas已将其作为混合值读入(即不仅仅是一个单一的基本数据类型,如一列所有整数)

<太长了,读不下去了,DANDAS博士从NUPY中获得了DHype(),它要求数据类型是固定宽度的,所以字符串被存储为指针的编号数组,因此对象

至于date列,您只需要首先将该列转换为datetime对象

dataframe['Starting Date'] = pd.to_datetime(dataframe['Starting Date'], infer_datetime_format=True)