OTHER OPERATIONS WITH DATES IN PANDAS
Listing B.17 shows the contents of pandas_misc1.py that shows how to extract a list of years from a column in a data frame.
Listing B.17: pandas_misc1.py
import pandas as pd
import numpy as np
df = pd.read_csv('multiple_dates.csv', parse_dates=['dates'])
print("df:")
print(df)
print()
year_list = df['dates']
arr1 = np.array([])
for long_year in year_list:
year = str(long_year)
short_year = year[0:4]
arr1 = np.append(arr1,short_year)
unique_years = set(arr1)
print("unique_years:")
print(unique_years)
print()
unique_arr = np.array(pd.Data frame.from_dict(unique_
years))
print("unique_arr:")
print(unique_arr)
print()
Listing B.17 initializes df with the contents of the CSV file multiple_dates.csv and then displays its contents. The next portion of Listing B.17 initializes year_list with the dates column of df.
The next code block contains a loop that iterates through the elements...