My project involves taking a wide array of SQL queries, converting them to dataframes in python via pandas, filtering by a user name column and then outputing the filtered dataframes to individual HTML files which are converted to PDFs and published.
I'm stuck on an issue where when the date columns appear in the HTML, they are YY-MM-DD-HH-MM-SS but i'd just like them to be YY-MM-DD to make the PDF output look cleaner.
I know that i can convert a single named column to the desired output using:
dataset['datecolumn'] = dataset['datecolumn'].dt.date
I did manage to solve the problem using the above approach by getting a list of the column names and then applying a for loop as below:
datecollist = list(dataset.select_dtypes(include=['datetime64']).columns)
for i in datecollist:
dataset[i] = dataset[i].dt.date
However I would really like to figure out a more pythonic way of fixing this problem as I want to avoid a further for loop in my code and to improve by python ability.
So far I attempted a mapper function without any luck:
mapper = lambda x: x.dt.date if isinstance(x, np.datetime64) else x
dataset.columns = dataset.columns.map(mapper)
It just seemed to skip out the columns that I'm looking to convert. I noticed that when I check the dtype of these columns they show up as:
in: dataset['First Date'].dtype
out: dtype('<M8[ns]')
Can anyone suggest where my mapper approach is going wrong, or show me a more efficient/cleaner way of solving the problem?