Why is pandas not formatting dates with date_format argument of to_csv?
pandas.DataFrame([datetime.datetime.now().date()]).to_csv(date_format="%Y %b")
',0\n0,2025-07-31\n'
Pandas doesn't support date - yet. Pandas was built on top of NumPy which only has a single 64-bit datetime type, datetime64[ns]. date values are stored as Objects.
ser=pd.Series([date.today()])
print(ser.dtype)
-----
object
If you try to format the value, you get an error
ser.dt.strftime("%Y %b")
---
...
AttributeError: Can only use .dt accessor with datetimelike values. Did you mean: 'at'?
You need to parse the object into a datetime first, then format it:
pd.to_datetime(ser).dt.strftime("%Y %b")
-----
0 2025 Jul
In the future Pandas will use Arrow as the backend, which does support dates. You can use it today as well if you specify the series type explicitly:
ser=pd.Series([date.today()],dtype="date32[pyarrow]")
ser.dt.strftime("%Y %b")
-----
0 2025 Jul
dtype: string[pyarrow]
If you use .dtypes to check what you have in dataframe then you should see that date is kept as object, but datetime is kept as datetime64[ns] and this can make difference.
import pandas as pd
import datetime
df = pd.DataFrame({
"date": [datetime.datetime.now().date()],
"datetime": [datetime.datetime.now()],
})
print(df.dtypes)
date object
datetime datetime64[ns]
But if you check single value then you see that this object keeps it as datetime.date() and you can still use .apply(lambda x:x.strftime("%Y %b") before sending to csv.
(but it doesn't allow to use .dt.strftime like for datetime64[ns])
df["date"] = df["date"].apply(lambda x:x.strftime("%Y %b"))
df["datetime"] = df["datetime"].dt.strftime("%Y %b")
to_csv() doesn't allow to set format for selected columns (with objects) so it has to be done before sending to file.
Because pandas only formats datetimes, not dates:
pandas.DataFrame([datetime.datetime.now()]).to_csv(date_format="%Y %b")
',0\n0,2025 Jul\n'
You can use pandas.to_datetime() to transform your date:
pandas.DataFrame([pandas.to_datetime(datetime.datetime.now().date())]).to_csv(date_format="%Y %b")
',0\n0,2025 Jul\n'
datetime. It's that Pandas until now only used Numpy types, and Numpy only has a single datetime type. This changes if you use the Arrow backend, which has a date type
.dtypesto check what you have in dataframe then you should see that date is kept asobject(probably string), but datetime is kept asdatetime64[ns]and maybe this makes difference.