pandas.date_range函数

语法:pandas.date_range(start=None, end=None, periods=None, freq='D', tz=None, normalize=False, name=None, closed=None, **kwargs)

该函数主要用于生成一个固定频率的时间索引,在调用构造方法时,必须指定start、end、periods中的两个参数值,否则报错。

主要参数说明:

periods:固定时期,取值为整数或None

freq:日期偏移量,取值为string或DateOffset,默认为'D'

normalize:若参数为True表示将start、end参数值正则化到午夜时间戳

name:生成时间索引对象的名称,取值为string或None

closed:可以理解成在closed=None情况下返回的结果中,若closed=‘left’表示在返回的结果基础上,再取左开右闭的结果,若closed='right'表示在返回的结果基础上,再取做闭右开的结果

[python]  view plain  copy
  1. In [11]: import pandas as pd  
  2.   
  3. In [12]: pd.date_range(start='20170101',end='20170110')  
  4. Out[12]:  
  5. DatetimeIndex(['2017-01-01''2017-01-02''2017-01-03''2017-01-04',  
  6.                '2017-01-05''2017-01-06''2017-01-07''2017-01-08',  
  7.                '2017-01-09''2017-01-10'],  
  8.               dtype='datetime64[ns]', freq='D')  
  9.   
  10. In [13]: pd.date_range(start='20170101',periods=10)  
  11. Out[13]:  
  12. DatetimeIndex(['2017-01-01''2017-01-02''2017-01-03''2017-01-04',  
  13.                '2017-01-05''2017-01-06''2017-01-07''2017-01-08',  
  14.                '2017-01-09''2017-01-10'],  
  15.               dtype='datetime64[ns]', freq='D')  
  16.   
  17. In [14]: pd.date_range(start='20170101',periods=10,freq='1D')  
  18. Out[14]:  
  19. DatetimeIndex(['2017-01-01''2017-01-02''2017-01-03''2017-01-04',  
  20.                '2017-01-05''2017-01-06''2017-01-07''2017-01-08',  
  21.                '2017-01-09''2017-01-10'],  
  22.               dtype='datetime64[ns]', freq='D')  
  23.   
  24. In [15]: pd.date_range(start='20170101',end='20170110',freq='3D',name='dt')  
  25. Out[15]: DatetimeIndex(['2017-01-01''2017-01-04''2017-01-07''2017-01-10'],  
  26.  dtype='datetime64[ns]', name='dt', freq='3D')  
  27.   
  28. In [16]: pd.date_range(start='2017-01-01 08:10:50',periods=10,freq='s',normaliz  
  29.     ...: e=True)  
  30. Out[16]:  
  31. DatetimeIndex(['2017-01-01 00:00:00''2017-01-01 00:00:01',  
  32.                '2017-01-01 00:00:02''2017-01-01 00:00:03',  
  33.                '2017-01-01 00:00:04''2017-01-01 00:00:05',  
  34.                '2017-01-01 00:00:06''2017-01-01 00:00:07',  
  35.                '2017-01-01 00:00:08''2017-01-01 00:00:09'],  
  36.               dtype='datetime64[ns]', freq='S')  
  37.   
  38. In [17]: pd.date_range(start='2017-01-01 08:10:50',end='2017-01-02 09:20:40',fr  
  39.     ...: eq='s',normalize=True)  
  40. Out[17]:  
  41. DatetimeIndex(['2017-01-01 00:00:00''2017-01-01 00:00:01',  
  42.                '2017-01-01 00:00:02''2017-01-01 00:00:03',  
  43.                '2017-01-01 00:00:04''2017-01-01 00:00:05',  
  44.                '2017-01-01 00:00:06''2017-01-01 00:00:07',  
  45.                '2017-01-01 00:00:08''2017-01-01 00:00:09',  
  46.                ...  
  47.                '2017-01-01 23:59:51''2017-01-01 23:59:52',  
  48.                '2017-01-01 23:59:53''2017-01-01 23:59:54',  
  49.                '2017-01-01 23:59:55''2017-01-01 23:59:56',  
  50.                '2017-01-01 23:59:57''2017-01-01 23:59:58',  
  51.                '2017-01-01 23:59:59''2017-01-02 00:00:00'],  
  52.               dtype='datetime64[ns]', length=86401, freq='S')  
  53.   
  54. In [18]: pd.date_range(start='2017-01-01 08:10:50',periods=15,freq='s',normaliz  
  55.     ...: e=False)  
  56. Out[18]:  
  57. DatetimeIndex(['2017-01-01 08:10:50''2017-01-01 08:10:51',  
  58.                '2017-01-01 08:10:52''2017-01-01 08:10:53',  
  59.                '2017-01-01 08:10:54''2017-01-01 08:10:55',  
  60.                '2017-01-01 08:10:56''2017-01-01 08:10:57',  
  61.                '2017-01-01 08:10:58''2017-01-01 08:10:59',  
  62.                '2017-01-01 08:11:00''2017-01-01 08:11:01',  
  63.                '2017-01-01 08:11:02''2017-01-01 08:11:03',  
  64.                '2017-01-01 08:11:04'],  
  65.               dtype='datetime64[ns]', freq='S')  
  66.   
  67. In [19]: pd.date_range(start='20170101',end='20170110',freq='3D',closed='left')  
  68.     ...:  
  69. Out[19]: DatetimeIndex(['2017-01-01''2017-01-04''2017-01-07'], dtype='dateti  
  70. me64[ns]', freq='3D')  
  71.   
  72. In [20]: pd.date_range(start='20170101',end='20170110',freq='3D',closed='right'  
  73.     ...: )  
  74. Out[20]: DatetimeIndex(['2017-01-04''2017-01-07''2017-01-10'], dtype='dateti  
  75. me64[ns]', freq='3D')  

你可能感兴趣的:(pandas,pandas)