python - 使用 Pandas Numpy Python 从开始和结束年份过滤日期时间集

标签 python arrays pandas numpy datetime

我如何编写一段代码来启用日期中的开始和结束年份。我想使用 pandas 或 numpy 来显示开始和结束年份。

import numpy as np 
import pandas as pd 

dates = ['2017-09-01 00:00:00', '2017-10-01 00:00:00', '2017-11-01 00:00:00', '2017-11-01 00:00:00', '2017-11-01 00:00:00', '2017-12-01 00:00:00', '2018-01-01 00:00:00', '2018-02-01 00:00:00', '2018-03-01 00:00:00', '2018-04-01 00:00:00', '2018-05-01 00:00:00', '2018-06-01 00:00:00', '2018-07-01 00:00:00', '2018-08-01 00:00:00', '2018-09-01 00:00:00', '2018-10-01 00:00:00', '2018-11-01 00:00:00', '2018-12-01 00:00:00', '2018-12-01 00:00:00', '2019-01-01 00:00:00']

预期输出:

Starting year: 2017
Ending year: 2019

最佳答案

根据您的情况,让我们来做

pd.Series(pd.to_datetime(dates)).dt.year.agg(StartYear = 'min', EndYear = 'max')
Out[245]: 
StartYear    2017
EndYear      2019
dtype: int64

关于python - 使用 Pandas Numpy Python 从开始和结束年份过滤日期时间集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69167398/

相关文章:

python - 无法用 Beautiful Soup 解析 html 表

python - 如何使用 Python Msqldb 模块?代替 %s 作为查询参数?

javascript - 使用 lodash/underscore.js 从数组中获取对象属性和值

python - 如何在 pandas 中多次旋转我的数据框,同时创建合并多个列的新列?

java - 如何消除错误 : illegal start of type in java?

arrays - 变量作为 for 循环中的标记

python - 如何使用不同的分隔符将Python中的列拆分为多列

Python:从非 BMP unicode 字符中查找等效代理对

python - Pandas 计算范围内每个值的出现次数

python - Pandas 数据框 : omit weekends and days near holidays