1

I was trying to obtain a formatted list of dates Pandas date_range and obtained an extra null string. This is using Pandas v2.2.2.

>>> import pandas as pd
>>> pd.date_range('2000-01-01','2000-01-03').format('%Y-%m-%d')
<stdin>:1: FutureWarning: DatetimeIndex.format is deprecated and will be removed in a future version. Convert using index.astype(str) or index.map(formatter) instead.
['', '2000-01-01', '2000-01-02', '2000-01-03']
1
  • 1
    As per the suggested duplicate, use strftime instead: pd.date_range('2000-01-01','2000-01-03').strftime('%Y-%m-%d').tolist(). To understand a correct (but unnecessary) use of format in this case, see here. Commented Jul 4, 2024 at 5:36

1 Answer 1

0

I found that this avoids the null string.

>>> list(pd.date_range('2000-01-01','2000-01-03').strftime('%Y-%m-%d').
astype(str))
['2000-01-01', '2000-01-02', '2000-01-03']

Remark: I used generative AI to help find the solution but this post itself was not created using generative AI.

Sign up to request clarification or add additional context in comments.

2 Comments

Use of generative AI is banned on SO, see policy.
I left the remark as I was unsure of how the policy would apply in this case. To clarify, I verified my own answer before posting. Happy for the mods to handle this as they see fit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.