简体   繁体   English

从时间索引的熊猫数据帧中获取两个时间戳内特定时间的值

[英]Getting values from time indexed pandas dataframe for a specific time within the two timestamps

I have the following pandas dataframe df:我有以下熊猫数据框 df:

                     C1  C2   C3
Date                             
2000-01-01 00:00:00   2  175  160
2000-01-01 01:00:00   4  192  164
2000-01-01 02:00:00   6  210  189
2000-01-01 03:00:00   8  217  199
2000-01-01 04:00:00  10  176  158

from which I need to get the value of C1, C2 and C3 for a specific datetime:我需要从中获取特定日期时间的 C1、C2 和 C3 的值:

import datetime
my_specific_time = str(datetime.datetime(2000, 1, 1, 1, 0, 0))
print(df['C1'].loc[mytime]) # prints 4

The problem is that I can only get values for the dates stored in the df.问题是我只能获取存储在 df 中的日期的值。 For example, getting the value of C1 for time 2000-01-01 01:30:00 is not possible unless I resample my dataframe:例如,除非我重新采样数据帧,否则无法获取时间2000-01-01 01:30:00C1值:

upsampled = df.resample('30min').ffill()
my_specific_time = str(datetime.datetime(2000, 1, 1, 1, 30, 0))
print(upsampled['C1'].loc[mytime]) # again prints 4

Please note that all the value of C1 between timespan of 2000-01-01 01:00:00 and 2000-01-01 02:00:00 is 4 .请注意,所有值C1的时间跨度之间2000-01-01 01:00:002000-01-01 02:00:004 Now the problem is that my_specific_time can be any random time and I would need to resample df using small enough values to be able to get the value for.现在的问题是my_specific_time可以是任何随机时间,我需要使用足够小的值重新采样 df 以便能够获得值。 I think this is not the best solution for this problem.我认为这不是这个问题的最佳解决方案。

While looking for possible solutions I only came across time spans in pandas but I did not quite understand how possibly I can use it in my problem.在寻找可能的解决方案时,我只遇到了 Pandas 的时间跨度,但我不太明白我有多大可能在我的问题中使用它。

Use DataFrame.asof method:使用DataFrame.asof方法:

print(df['C1'].asof(my_specific_time))

4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Pandas 的时间范围内获取特定值 - Getting specific values from a range of time in Pandas 从时间索引的pandas数据帧中删除夏令时行 - Deleting rows of daylight saving time from a time indexed pandas dataframe 在特定时间范围内来自熊猫数据帧的值的总和 - Sum set of values from pandas dataframe within certain time frame 来自两个时间值之间的熊猫数据帧的行 - Rows from pandas dataframe between two time values 大熊猫数据帧中时间戳之间的时间差作为直方图 - time difference between timestamps in a pandas dataframe as histogram 获取时间戳在特定滑动 window 时间间隔 pandas (时间序列)内的行 - Get rows whose timestamps are within specific sliding window time interval pandas (Time Series) 通过时间比较两列内的值获取日期 - Getting the date from a comparison of values within two columns through time 熊猫:日期时间索引系列到时间索引日期列的数据框 - Pandas: datetime indexed series to time indexed date columns dataframe 映射两个数据帧,计算第二个数据帧中的时间戳在第一个数据帧的日期时间范围内的事件 - Map two dataframes, count events where timestamps in second dataframe are within the date-time ranges of the first dataframe 从另一个数据框中的行中同时选择两个值从熊猫数据框中的行 - Select rows from pandas dataframe by two values at the same time from rows in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM