Python practice data analysis 100 knocks I was dealing with date data on pandas. I used pandas.to_datetime () to convert the date of the string to the date type data, but I converted it properly without showing the format etc. and it worked. I wasn't sure what format would work, so I checked it.
>>> import pandas as pd
>>> from datetime import datetime
#Pattern separated by a hyphen
>>> pd.to_datetime("2019-12-31")
Timestamp('2019-12-31 00:00:00')
# datetime.today()OK
>>> pd.to_datetime(datetime.today())
Timestamp('2019-12-31 09:56:05.590997')
#Dates are OK without delimiters
>>> pd.to_datetime("20191231 13:00")
Timestamp('2019-12-31 13:00:00')
>>> pd.to_datetime("20191231")
Timestamp('2019-12-31 00:00:00')
#If it is before 2000, the order of year, month, and day in the second digit of the Christian era is OK, separated by slashes.
>>> pd.to_datetime("83/12/31")
Timestamp('1983-12-31 00:00:00')
#Of course, future dates are OK
>>> pd.to_datetime("2038/12/31 12:35:56.55457")
Timestamp('2038-12-31 12:35:56.554570')
#If only dates separated by slashes are used, they will be interpreted as days, months, and years in Western style.
>>> pd.to_datetime("19/12/31")
Timestamp('2031-12-19 00:00:00')
If you write the year in two digits in the Christian era, it seems that you will be scooped up in an unexpected place by conversion. Other than that, it seems to be relatively flexible. If you enter the creation date etc. in the file name with 4 digits in the Christian era, it will be easier to aggregate later. After that, datetime.today () is also received, so it may be good for logging.
If you want to convert explicitly, you can add the format option. For details, please see the official document below. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html