CONVERTING STRINGS TO DATES IN PANDAS
Listing B.12 shows the content of string2date.py, which illustrates how to convert strings to date formats.
Listing B.12: string2date.py
import pandas as pd
bdates1 = {'strdates': ['20210413','20210813','20211225'],
'people': ['Sally','Steve','Sarah']
}
df1 = pd.Data frame(bdates1, columns =
['strdates','people'])
df1['dates'] = pd.to_datetime(df1['strdates'],
format='%Y%m%d')
print("=> Contents of data frame df1:")
print(df1)
print()
print(df1.dtypes)
print()
bdates2 = {'strdates': ['13Apr2021','08Aug2021','25D
ec2021'],
'people': ['Sally','Steve','Sarah']
}
df2 = pd.Data frame(bdates2, columns =
['strdates','people'])
df2['dates'] = pd.to_datetime(df2...