Proposal to Add a datetimewalker Function to the datetime Module

Hi everyone,

I’m working on a proposal to add a new function to the datetime module, which functions similarly to the built-in range function, but for datetime objects.

Function Overview

The datetimewalker function would allow users to generate a sequence of datetime objects between a start and end date with a specified timedelta interval.

Example:

from datetime import datetime, timedelta, datetimewalker

start = datetime(2024, 8, 25)
end = datetime(2024, 8, 30)
delta = timedelta(days=1)

for i in datetimewalker(start, end, delta):
    print(i)

output:

2024-08-25 00:00:00
2024-08-26 00:00:00
2024-08-27 00:00:00
2024-08-28 00:00:00
2024-08-29 00:00:00

Next Steps

I have implemented this function and am considering creating an issue to start the contribution process. Before I do that, I wanted to get feedback from the community. Any advice or suggestions would be greatly appreciated!

Thanks!

2 Likes

I like the concept!
The name not so much, but that’s bike shedding

Also, can we have a default step interval of 1 day?

2 Likes

I agree. I’m not good at naming! xD
and yes, default interval is 1 day

Is something akin to DateTimeRange · PyPI or python-dateutil · PyPI not appropriate?

A

2 Likes

FWIW, a similar functions exists in pandas: pandas.date_range — pandas 2.2.2 documentation

>>> pd.date_range("2024-08-25", "2024-08-30", inclusive="left")
DatetimeIndex(['2024-08-25', '2024-08-26', '2024-08-27', '2024-08-28',
               '2024-08-29'],
              dtype='datetime64[ns]', freq='D')

You can also do something similar with NumPy’s arange:

>>> np.arange("2024-08-25", "2024-08-30", dtype="datetime64[D]")
array(['2024-08-25', '2024-08-26', '2024-08-27', '2024-08-28',
       '2024-08-29'], dtype='datetime64[D]')
2 Likes

There is also more_itertools.numeric_range which you can use in a very similar way.

In fact, you can just change your import, so that you import datetimewalker differently and your code sample already works.

In [1]: from datetime import datetime, timedelta
   ...: from more_itertools import numeric_range as datetimewalker
   ...:
   ...: start = datetime(2024, 8, 25)
   ...: end = datetime(2024, 8, 30)
   ...: delta = timedelta(days=1)
   ...:
   ...: for i in datetimewalker(start, end, delta):
   ...:     print(i)
   ...:
2024-08-25 00:00:00
2024-08-26 00:00:00
2024-08-27 00:00:00
2024-08-28 00:00:00
2024-08-29 00:00:00
3 Likes

Thank you all for your feedback and suggestions. I appreciate the references to existing solutions using packages like numpy, pandas, more_itertools, and others.

While it’s true that these packages offer powerful tools for working with date ranges and many other tasks, I feel that the functionality provided by datetimewalker is simple and basic enough that it could be beneficial to have it directly available in the datetime module without needing to install any additional packages.

The idea is to provide a straightforward, built-in solution that could be useful in many everyday scenarios, especially when users might not want to bring in a whole library just for a simple date range iteration. I believe that integrating this into the standard library would make this common task more accessible and convenient for everyone.

Again, thank you for the insights—it’s great to hear different perspectives on this!

1 Like

Nothing with datetime is simple.

What should be the correct behavior be with regards to stuff like daylight saving times, leap seconds or a timezone being moved around? Should the difference always be exactly one day? Should the lower parts of the datetime stay the same?

I don’t think we should add a naive version to the stdlib version - Someone should make a survey of various real world usecases and write a PEP suggesting a solution that covers as many of these as possible. I don’t really know what this solution would be, but I am pretty sure you did not do the research for this (if you did, please show it).

It would probably best to add an rrule implementation to the stdlib somewhere (e.g. rrule — dateutil 3.9.0 documentation)

6 Likes