[英]How to check if the time-series belongs to last year using pandas?
I am making an app where the users can upload their time-series CSV data.我正在制作一个应用程序,用户可以在其中上传他们的时间序列 CSV 数据。 I want the user to always upload last year's data (in 2022, the time-series should be of 2021; in 2023 the data should be of 2022 and so on) because of which I have to do a check if the data is from last year or not.
我希望用户总是上传去年的数据(在 2022 年,时间序列应该是 2021 年;在 2023 年,数据应该是 2022 年等等)因为我必须检查数据是否来自最后年与否。
Is there a way I can do this check using pandas while reading the csv (I read the csv by doing pd.read_csv(my_file)
)?有没有一种方法可以在读取 csv 时使用 pandas 进行检查(我通过执行
pd.read_csv(my_file)
读取 csv )?
Sample of time-series时间序列样本
dates values
0 2021-01-01 01:00:00 371.428
1 2021-01-01 02:00:00 390.194
2 2021-01-01 03:00:00 349.924
3 2021-01-01 04:00:00 342.886
4 2021-01-01 05:00:00 331.157
.
.
.
.
8779 2021-12-31 20:00:00 515.307
8780 2021-12-31 21:00:00 432.811
8781 2021-12-31 22:00:00 421.082
8782 2021-12-31 23:00:00 394.886
8783 2022-01-01 00:00:00 373.773
The last row will always be of current year at 00:00
最后一行将始终是当前年份的
00:00
I think no, need first read values.我认为不,需要先读取值。 You can convert values to DataFrame first and then compare years by
Series.dt.year
with Timestamp.year
subtracted 1
and for test if all values match use Series.all
:您可以先将值转换为 DataFrame,然后通过
Series.dt.year
与Timestamp.year
减去1
比较年份,并测试所有值是否匹配使用Series.all
:
df = pd.read_csv(my_file, parse_dates=['dates'])
test = df['dates'].dt.year.iloc[:-1].eq(pd.Timestamp('now').year - 1).all()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.