[PYTHON] How to use pandas Timestamp and date_range

How to use pandas Timestamp and date_range

pd.Timestamp(ts_input, offset=None, tz=None, unit=None) You can type pd.Timestamp in the yyyymmdd format, and it will separate the datetime with spaces, hyphens, and other appropriate delimiters. The 6-digit number is in ddmmyy format.

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) Iterates from date start to end in Timestamp format pd.date_range ()

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')

Generate 20 pieces every hour from '2014-11-01 10:00'

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')

Generated every hour from 2014-11-01 10:00'to' 2014-11-02 10:00'

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')

Generated every 2 hours from '2014-11-01 10:00' to '2014-11-02' 10:00

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')

reference

StatsFragments Python, R, Rust, Statistics, Machine Learning and Python pandas make date and time related data manipulation easy

Recommended Posts

How to use pandas Timestamp and date_range
How to use Pandas 2
How to use Pandas Rolling
How to install and use Tesseract-OCR
[Python] How to use Pandas Series
How to use .bash_profile and .bashrc
How to install and use Graphviz
[Python] Summary of how to use pandas
How to install and use pandas_datareader [Python]
[Pandas] What is set_option [How to use]
python: How to use locals () and globals ()
How to use Python zip and enumerate
How to use is and == in Python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use TokyoTechFes2015
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use lists, tuples, dictionaries, and sets
[Python] How to use hash function and tuple.
How to install Cascade detector and how to use it
How to use Qt Designer
[Python] [Django] How to use ChoiceField and how to add options
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
Key additions to pandas 1.1.0 and 1.0.0
How to use Python argparse
How to use IPython Notebook
[Note] How to use virtualenv
How to use redis-py Dictionaries
How to extract null values and non-null values with pandas
Python: How to use pydub