Python| pandas.date_range() 方法
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
pandas.date_range()
是 Pandas 中的通用函数之一,用于返回固定频率的 DatetimeIndex。
Syntax: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
Parameters:
start : Left bound for generating dates.
end : Right bound for generating dates.
periods : Number of periods to generate.
freq : Frequency strings can have multiples, e.g. ‘5H’. See here for a list of frequency aliases.
tz : Time zone name for returning localized DatetimeIndex. By default, the resulting DatetimeIndex is timezone-naive.
normalize : Normalize start/end dates to midnight before generating date range.
name : Name of the resulting DatetimeIndex.
closed : Make the interval closed with respect to the given frequency to the ‘left’, ‘right’, or both sides (None, the default).
Returns: DatetimeIndex
代码#1:
# importing pandas as pd
import pandas as pd
per1 = pd.date_range(start ='1-1-2018',
end ='1-05-2018', freq ='5H')
for val in per1:
print(val)
输出:
代码#2:
# importing pandas as pd
import pandas as pd
dRan1 = pd.date_range(start ='1-1-2018',
end ='8-01-2018', freq ='M')
dRan2 = pd.date_range(start ='1-1-2018',
end ='11-01-2018', freq ='3M')
print(dRan1, '\n\n', dRan2)
输出:
代码#3:
# importing pandas as pd
import pandas as pd
# Specify start and periods, the number of periods (days).
dRan1 = pd.date_range(start ='1-1-2018', periods = 13)
# Specify end and periods, the number of periods (days).
dRan2 = pd.date_range(end ='1-1-2018', periods = 13)
# Specify start, end, and periods; the frequency
# is generated automatically (linearly spaced).
dRan3 = pd.date_range(start ='01-03-2017',
end ='1-1-2018', periods = 13)
print(dRan1, "\n\n", dRan2, '\n\n', dRan3)
输出:
代码 #4:
# importing pandas as pd
import pandas as pd
# Specify start and periods, the number of periods (days).
dRan1 = pd.date_range(start ='1-1-2018',
periods = 13, tz ='Asia / Tokyo')
dRan1
输出: