如何使用Pandas打印从给定日期开始的n天的日期?
在这篇文章中,我们将打印从给定日期开始的n个天数的所有日期。这可以用pandas.date_range()函数来完成。这个函数用来获得一个固定频率的DatetimeIndex。
语法: pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)
步骤:
- 导入pandas模块
- 创建一个参数函数,用于计算起始日期和周期之间的日期序列。
- 用函数中的pandas.date_range()生成起点和终点之间的日期序列。
- 存储到pandas系列的函数内
- 返回大Pandas系列。
以下是实现。
# Importing modules
import pandas as pd
# creating function
def Time_series(date, per):
# computing date range with date
# and given periods
date_series = pd.date_range(date, periods=per)
# creating series for date_range
Result = pd.Series(date_series)
print(Result)
# Driver Code
# Date in the YYYY-MM-DD format
date = "2020-03-01"
# Number of times the date is
# needed to be printed
per = 10
Time_series(date, per)
输出 :
0 2020-03-01
1 2020-03-02
2 2020-03-03
3 2020-03-04
4 2020-03-05
5 2020-03-06
6 2020-03-07
7 2020-03-08
8 2020-03-09
9 2020-03-10
dtype: datetime64[ns]