I want a list of date range in which each element is 'yyyymmdd'
format string, such as : ['20130226','20130227','20130228','20130301','20130302']
.
I can use pandas to do so:
>>> pandas.date_range('20130226','20130302')
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-02-26 00:00:00, ..., 2013-03-02 00:00:00]
Length: 5, Freq: D, Timezone: None
But it is DatetimeIndex and I need to do some extra format transform, so how to do that in a neat way ?
Using format
:
>>> r = pandas.date_range('20130226','20130302')
>>> r.format(formatter=lambda x: x.strftime('%Y%m%d'))
['20130226', '20130227', '20130228', '20130301', '20130302']
or using map
:
>>> r.map(lambda x: x.strftime('%Y%m%d'))
array(['20130226', '20130227', '20130228', '20130301', '20130302'], dtype=object)
Or using list comprehension:
[d.strftime('%Y%m%d') for d in pandas.date_range('20130226','20130302')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With