【发布时间】:2019-04-16 23:59:19
【问题描述】:
我需要将数据框 colmun 转换为 pandas 中的日期时间。 但是,它的数据类型不是我所期望的。
>>> df = pd.DataFrame(['2009-11-27'], columns = ['my_col'])
>>> df.dtypes['my_col']
dtype('O')
>>> pd.to_datetime(df['my_col'], format='%Y-%m-%d', errors = 'coerce')
0 2009-11-27
Name: my_col, dtype: datetime64[ns]
>>> df.dtypes['my_col']
dtype('O')
>>> df['my_col'] = pd.to_datetime(df['my_col'], format='%Y-%m-%d', errors = 'coerce')
>>> df.dtypes['my_col']
dtype('<M8[ns]')
>>> df['my_col'] = pd.to_datetime(df['my_col'], format='%Y-%m-%d', errors = 'coerce')
>>> df.dtypes['my_col']
dtype('<M8[ns]')
>>> df = pd.DataFrame(['2009-11-27'], columns = ['my_col'])
>>> df['my_col'] = pd.to_datetime(df['my_col'], format='%Y-%m-%d', errors = 'coerce')
>>> df.dtypes['my_col']
dtype('<M8[ns]')
我需要获取列的数据类型,以便我的代码可以做
if df.dtypes['my_col'] in (np.datetime64, datetime.datetime):
do something .....
有什么建议吗?
【问题讨论】:
标签: python python-3.x pandas datetime dataframe