pandas Timestampとdate_rangeの使い方

# pandas Timestampとdate_rangeの使い方

## pd.Timestamp(ts_input, offset=None, tz=None, unit=None)
`pd.Timestamp`はyyyymmdd形式で打ってもいいし、スペースとかハイフンとか適当な区切りでdatetime区切ってくれる。
6桁の数字はddmmyy形式となる。

```python
import pandas as pd

a=pd.Timestamp('2016-2-1')
 # [Out]# Timestamp('2016-02-01 00:00:00')

b=pd.Timestamp('20160301')
 # [Out]# Timestamp('2016-03-01 00:00:00')

pd.Timestamp('160301')
 # [Out]# Timestamp('2001-03-16 00:00:00')
```




## pd.date_range(start=None, end=None, periods=None, freq='D', tz=None, normalize=False, name=None, closed=None, **kwargs)
Timestamp形式の日付startからendまでイテレートするpd.date_range()

```python
pd.date_range('20160201','20160301')
 # [Out]# DatetimeIndex(['2016-02-01', '2016-02-02', '2016-02-03', '2016-02-04',
 # [Out]#                '2016-02-05', '2016-02-06', '2016-02-07', '2016-02-08',
 # [Out]#                '2016-02-09', '2016-02-10', '2016-02-11', '2016-02-12',
 # [Out]#                '2016-02-13', '2016-02-14', '2016-02-15', '2016-02-16',
 # [Out]#                '2016-02-17', '2016-02-18', '2016-02-19', '2016-02-20',
 # [Out]#                '2016-02-21', '2016-02-22', '2016-02-23', '2016-02-24',
 # [Out]#                '2016-02-25', '2016-02-26', '2016-02-27', '2016-02-28',
 # [Out]#                '2016-02-29', '2016-03-01'],
 # [Out]#               dtype='datetime64[ns]', freq='D')
```







## '2014-11-01 10:00'から1時間おきに20個生成

```python
pd.date_range('2014-11-01 10:00',periods=20,freq='H')

 # [Out]# DatetimeIndex(['2014-11-01 10:00:00', '2014-11-01 11:00:00',
 # [Out]#                '2014-11-01 12:00:00', '2014-11-01 13:00:00',
 # [Out]#                '2014-11-01 14:00:00', '2014-11-01 15:00:00',
 # [Out]#                '2014-11-01 16:00:00', '2014-11-01 17:00:00',
 # [Out]#                '2014-11-01 18:00:00', '2014-11-01 19:00:00',
 # [Out]#                '2014-11-01 20:00:00', '2014-11-01 21:00:00',
 # [Out]#                '2014-11-01 22:00:00', '2014-11-01 23:00:00',
 # [Out]#                '2014-11-02 00:00:00', '2014-11-02 01:00:00',
 # [Out]#                '2014-11-02 02:00:00', '2014-11-02 03:00:00',
 # [Out]#                '2014-11-02 04:00:00', '2014-11-02 05:00:00'],
 # [Out]#               dtype='datetime64[ns]', freq='H')
```




## 2014-11-01 10:00'から'2014-11-02 10:00'まで1時間おきに生成
```python
pd.date_range('2014-11-01 10:00','2014-11-02 10:00',freq='H')

 # [Out]# DatetimeIndex(['2014-11-01 10:00:00', '2014-11-01 11:00:00',
 # [Out]#                '2014-11-01 12:00:00', '2014-11-01 13:00:00',
 # [Out]#                '2014-11-01 14:00:00', '2014-11-01 15:00:00',
 # [Out]#                '2014-11-01 16:00:00', '2014-11-01 17:00:00',
 # [Out]#                '2014-11-01 18:00:00', '2014-11-01 19:00:00',
 # [Out]#                '2014-11-01 20:00:00', '2014-11-01 21:00:00',
 # [Out]#                '2014-11-01 22:00:00', '2014-11-01 23:00:00',
 # [Out]#                '2014-11-02 00:00:00', '2014-11-02 01:00:00',
 # [Out]#                '2014-11-02 02:00:00', '2014-11-02 03:00:00',
 # [Out]#                '2014-11-02 04:00:00', '2014-11-02 05:00:00',
 # [Out]#                '2014-11-02 06:00:00', '2014-11-02 07:00:00',
 # [Out]#                '2014-11-02 08:00:00', '2014-11-02 09:00:00',
 # [Out]#                '2014-11-02 10:00:00'],
 # [Out]#               dtype='datetime64[ns]', freq='H')
```




## '2014-11-01 10:00'から'2014-11-02' 10:00まで2時間おきに生成

```python
d.date_range('2014-11-01 10:00','2014-11-02 10:00',freq='2H')

 # [Out]# DatetimeIndex(['2014-11-01 10:00:00', '2014-11-01 12:00:00',
 # [Out]#                '2014-11-01 14:00:00', '2014-11-01 16:00:00',
 # [Out]#                '2014-11-01 18:00:00', '2014-11-01 20:00:00',
 # [Out]#                '2014-11-01 22:00:00', '2014-11-02 00:00:00',
 # [Out]#                '2014-11-02 02:00:00', '2014-11-02 04:00:00',
 # [Out]#                '2014-11-02 06:00:00', '2014-11-02 08:00:00',
 # [Out]#                '2014-11-02 10:00:00'],
 # [Out]#               dtype='datetime64[ns]', freq='2H')
```




# 参考
[StatsFragments Python, R, Rust, 統計, 機械学習とか Python pandas で日時関連のデータ操作をカンタンに](http://sinhrks.hatenablog.com/entry/2014/11/09/183603)