4

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'
3
  • 2
    it looks like you ask question only to write own answer. Commented Jul 31 at 13:47
  • 3
    @furas May look fishy, but is technically okay in general (quote: If you have a question that you already know the answer to, and you would like to document that knowledge in public so that others (including yourself) can find it later, it's perfectly okay to ask and answer your own question on a Stack Exchange site) – even encouraged by the "Answer your own question" mechanism (see link). Commented Jul 31 at 13:50
  • 2
    if you use .dtypes to check what you have in dataframe then you should see that date is kept as object (probably string), but datetime is kept as datetime64[ns] and maybe this makes difference. Commented Jul 31 at 13:54

3 Answers 3

4

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]
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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'

1 Comment

There are a lot of identical questions and the real reason is not that Pandas only formats 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.