A data frame:
import pandas as pd
data = [['tom', 10, pd.to_datetime('2021-01-01')], ['nick', 15, pd.to_datetime('2021-01-02')], ['juli', 14, pd.to_datetime('2021-01-01')]]
df = pd.DataFrame(data, columns=['Name', 'Age', 'date'])
Here's how it looks in pycharm:
It includes the time part 00:00:00
. This is relevant because when I try to display in a datatable with Dash, it displays with this part. So I want to get rid of the time part and show just the date part.
Here's the .info:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 date 3 non-null datetime64[ns]
dtypes: datetime64[ns](1), int64(1), object(1)
memory usage: 200.0+ bytes
I would like to convert date to just a string of the form '2021-01-01', '%Y-%m-%d'.
Tried:
df['newdate'] = df['date'].strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/venv/lib/python3.8/site-packages/pandas/core/generic.py", line 5460, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'strftime'
How can I reformat df['date'] to be a string of the form '2021-01-01', that is '%Y-%m-%d'?