熊猫:欧洲风格的date_range

时间:2016-07-25 10:40:57

标签: python python-2.7 pandas 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参数得到相同的输出。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您只能使用DatetimeIndex.strftime,但丢失了DatetimeIndex并获得了list strings

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并转换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')