python - Pandas 的 to_datetime 函数不会改变 dtype

标签 python pandas

我最近一直在使用 python,我发现了一个我似乎无法解决的问题。我正在使用 Pandas 数据集,当我想使用 to_datetime 函数将变量的数据类型从“对象”更改为“datetime64”时,它不会将其更改为所需的“datetime64”数据类型。

到目前为止,我只尝试了 to_datetime 函数,但这似乎无法解决问题。我正在寻找使 to_datetime 工作的解决方案或任何其他可以将我的变量的数据类型从“对象”更改为“datetime64”的代码

在这里您可以找到有关数据集的信息:

df.head()
Formatted Date                      Summary  Precip Type Temperature (C)   Apparent Temperature (C)   Humidity   Wind Speed (km/h)   Wind Bearing (degrees)  Visibility (km)  Loud Cover Pressure (millibars)   Daily Summary
0   2006-04-01 00:00:00.000 +0200   Partly Cloudy   rain    9.472222    7.388889    0.89    14.1197     251.0   15.8263     0.0     1015.13     Partly cloudy throughout the day.
1   2006-04-01 01:00:00.000 +0200   Partly Cloudy   rain    9.355556    7.227778    0.86    14.2646     259.0   15.8263     0.0     1015.63     Partly cloudy throughout the day.
2   2006-04-01 02:00:00.000 +0200   Mostly Cloudy   rain    9.377778    9.377778    0.89    3.9284  204.0   14.9569     0.0     1015.94     Partly cloudy throughout the day.
3   2006-04-01 03:00:00.000 +0200   Partly Cloudy   rain    8.288889    5.944444    0.83    14.1036     269.0   15.8263     0.0     1016.41     Partly cloudy throughout the day.
4   2006-04-01 04:00:00.000 +0200   Mostly Cloudy   rain    8.755556    6.977778    0.83    11.0446     259.0   15.8263     0.0     1016.51     Partly cloudy throughout the day.

在这里您可以看到使用 to_datetime 函数之前的数据类型:

df.dtypes
Formatted Date               object
Summary                      object
Precip Type                  object
Temperature (C)             float64
Apparent Temperature (C)    float64
Humidity                    float64
Wind Speed (km/h)           float64
Wind Bearing (degrees)      float64
Visibility (km)             float64
Loud Cover                  float64
Pressure (millibars)        float64
Daily Summary                object
dtype: object

使用 to_datetime 函数后:

df['Date'] = pd.to_datetime(df['Formatted Date'])
df.dtypes

Formatted Date               object
Summary                      object
Precip Type                  object
Temperature (C)             float64
Apparent Temperature (C)    float64
Humidity                    float64
Wind Speed (km/h)           float64
Wind Bearing (degrees)      float64
Visibility (km)             float64
Loud Cover                  float64
Pressure (millibars)        float64
Daily Summary                object
Date                         object
dtype: object

你能告诉我我做错了什么吗? 提前致谢!

最佳答案

对于pandas>=0.24,您需要添加参数utc=True

import pandas as pd

# load dataset
df = pd.read_csv('weatherHistory.csv')

df['Date'] = df['Formatted Date'].apply(pd.to_datetime, utc=True)
df.dtypes
Formatted Date                           object
Summary                                  object
Precip Type                              object
Temperature (C)                         float64
Apparent Temperature (C)                float64
Humidity                                float64
Wind Speed (km/h)                       float64
Wind Bearing (degrees)                  float64
Visibility (km)                         float64
Loud Cover                              float64
Pressure (millibars)                    float64
Daily Summary                            object
Date                        datetime64[ns, UTC]

关于python - Pandas 的 to_datetime 函数不会改变 dtype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57708107/

相关文章:

python - 计算数据框列中列表项的出现次数,按另一列分组

Python Pandas 日期时间索引

python - 检查终端中的 .csv 文件有多少行

python - 用于在标记内查找短语的正则表达式

python - Try/except 检查字符串

python - Pandas - 删除列中已存在于另一列中的部分字符串

python - 加速 pandas csv 读取和后续的 downcast

python - 谷歌存储 python API - 上传一个 StringIO 对象

python - 如何在python中抓取td标签内的链接

python - 如何绘制一列平均值的直方图,而 bin 由 Pandas 中的另一列定义