So when I using pd.to_datetime(),it raised some errors like format not
matching and not timelike data. How can I unify the format of this
column?
Use the errors='coerce'
option in order to return NaT
(Not a Time) for non-converted values. Also note that the format
argument is not required. Omitting it will enable Pandas to try multiple formats, failing which it will revert to NaT
1. For example:
df['datetime'] = pd.to_datetime(df['datetime'], errors='coerce')
Beware, mixed types may be interpreted incorrectly. For example, how will Python know whether 05/06/2018
is 5th June or 6th May? An order of conventions will be applied and if you need greater control you will need to apply a customised ordering yourself.
Can I keep the datetime dtype, and change the format into '%m/%d/%Y'?
No, you cannot. datetime
series are stored internally as integers. Any human-readable date representation is just that, a representation, not the underlying integer. To access your custom formatting, you can use methods available in Pandas. You can even store such a text representation in a pd.Series
variable:
formatted_dates = df['datetime'].dt.strftime('%m/%d/%Y')
The dtype
of formatted_dates
will be object
, which indicates that the elements of your series point to arbitrary Python times. In this case, those arbitrary types happen to be all strings.
Lastly, I strongly recommend you do not convert a datetime
series to strings until the very last step in your workflow. This is because as soon as you do so, you will no longer be able to use efficient, vectorised operations on such a series.
1 This will sacrifice performance and contrasts with datetime.strptime
, which requires format to be specified. Internally, Pandas uses the dateutil
library, as indicated in the docs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…