Pandas:欧式date_range

Pandas: European style date_range

我正在尝试生成 date_range 欧洲风格 dd/mm/yyyy

import pandas as pd

rng = pd.date_range(start = '1/09/2016', periods = 10, dayfirst = True)

输出结果如下:

DatetimeIndex(['2016-01-09', '2016-01-10', '2016-01-11', '2016-01-12',
           '2016-01-13', '2016-01-14', '2016-01-15', '2016-01-16',
           '2016-01-17', '2016-01-18'],
          dtype='datetime64[ns]', freq='D')

我在没有 dayfirst 参数的情况下得到相同的输出。

我做错了什么?

您只能使用 DatetimeIndex.strftime,但丢失 DatetimeIndex 并获得 stringslist:

print (rng.strftime('%d/%m/%Y'))
['09/01/2016' '10/01/2016' '11/01/2016' '12/01/2016' '13/01/2016'
 '14/01/2016' '15/01/2016' '16/01/2016' '17/01/2016' '18/01/2016']

通过评论编辑:

如果需要每日频率,将参数freq添加到date_range and convert to_datetime date:

import pandas as pd

rng = pd.date_range(start = pd.to_datetime('1/09/2016',  dayfirst = True), 
                    periods = 10, freq = 'D')
print (rng)
DatetimeIndex(['2016-09-01', '2016-09-02', '2016-09-03', '2016-09-04',
               '2016-09-05', '2016-09-06', '2016-09-07', '2016-09-08',
               '2016-09-09', '2016-09-10'],
              dtype='datetime64[ns]', freq='D')