I have a pandas dataframe that has a datetime column called "time". From this column, I only want to extract the year-month-date in the syntax it is given in, and I want to create a new column with that value as a string. Please help!
Asked
Active
Viewed 1,563 times
1
-
Does this answer your question? [Convient way to convert int(yyyymmdd) to datetime object and str](https://stackoverflow.com/questions/66038743/convient-way-to-convert-intyyyymmdd-to-datetime-object-and-str) – ti7 Mar 03 '21 at 00:57
-
Does this answer your question? [Python datetime to string without microsecond component](https://stackoverflow.com/questions/7999935/python-datetime-to-string-without-microsecond-component) – Scinana Mar 03 '21 at 01:09
-
In general, please add data/samples as text, not image. This makes it much easier to give good answers, based on your example. – FObersteiner Mar 03 '21 at 06:18
1 Answers
1
IIUC pandas.Series.dt.strftime
is what you want:
df["new_time"] = df["time"].dt.strftime("%Y-%m-%d")
Also, check this documentation where you can find the datetime format codes.

Pablo C
- 4,661
- 2
- 8
- 24