我的数据库中有一列表示句点,但该列由对象类型的数据组成,我需要将它们转换为日期类型。
柱子看起来有点像
周期 |
---|
2022/07 |
2022/06 |
1993/02 |
1993/01 |
try:
df['Period'] = pd.to_datetime(df['Period'],format='%Y/%m').dt.date
您有三个处理错误的选项:
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
- If :const:`'raise'`, then invalid parsing will raise an exception.
- If :const:`'coerce'`, then invalid parsing will be set as :const:`NaT`.
- If :const:`'ignore'`, then invalid parsing will return the input.
您可以使用pd.to_datetime
pd.to_datetime(df.Period, format='%Y/%m', errors="coerce").dt.date
多次使用不同的格式。