如何使用分钟和秒按 pandas 中的 datetime64 列过滤数据框?
How do I filter a dataframe by a datetime64 column in pandas using minutes and seconds?
我已将类型为 'str' 的时间列转换为 datetime64:
try:
heart_rate_seconds['time'] = pd.to_datetime(heart_rate_seconds['time'], format = "%m/%d/%Y %I:%M:%S %p")
except:
heart_rate_seconds['time'] = pd.to_datetime(heart_rate_seconds['time'], format = "%Y/%m/%d %H:%M:%S")
如何过滤数据帧,使其只包含时间列中分和秒为零的行?
这是我试过的:
type(heart_rate_seconds['time'][0])
这显示pandas._libs.tslibs.timestamps.Timestamp
test = heart_rate_seconds['time'][1].second
test
这个有效
heart_rate_hourly = heart_rate_seconds.loc[heart_rate_seconds['time'].dt.second() == 0
and heart_rate_seconds['time'].dt.minute() == 0]
出现错误:TypeError: 'Series' object is not callable
heart_rate_hourly = heart_rate_seconds[heart_rate_seconds['time'].dt.strftime("%M") == 0
and heart_rate_seconds['time'].dt.strftime("%S") == 0]
出现错误:ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
on_the_hour = []
for time in heart_rate_seconds['time']:
if time.second == 0 and time.minute == 0:
on_the_hour.append(time)
on_the_hour[:5]
这个有效
on_the_hour = []
for row in heart_rate_seconds:
time = row[1]
if time.second == 0 and time.minute == 0:
on_the_hour.append(time)
on_the_hour[:5]
这不起作用 - AttributeError:'str' 对象没有属性 'second'
谢谢
您可以使用 dt
accessor 为某些属性创建掩码。例如:
import pandas as pd
df = pd.DataFrame({'time': ['01/03/2021 03:30:00 AM', '2021/02/04 13:00:00'],
'values': [0, 1]})
# note that pandas automatically infers the format correctly:
df['time'] = pd.to_datetime(df['time'])
# a mask where min and sec are zero:
m = (df['time'].dt.minute == 0) & (df['time'].dt.second == 0)
print(df[m])
# time values
# 1 2021-02-04 13:00:00 1
请注意,您不必将条件分配给变量就可以使用它们 - 当然您也可以在 loc
.
中使用它们
我已将类型为 'str' 的时间列转换为 datetime64:
try:
heart_rate_seconds['time'] = pd.to_datetime(heart_rate_seconds['time'], format = "%m/%d/%Y %I:%M:%S %p")
except:
heart_rate_seconds['time'] = pd.to_datetime(heart_rate_seconds['time'], format = "%Y/%m/%d %H:%M:%S")
如何过滤数据帧,使其只包含时间列中分和秒为零的行?
这是我试过的:
type(heart_rate_seconds['time'][0])
这显示pandas._libs.tslibs.timestamps.Timestamp
test = heart_rate_seconds['time'][1].second
test
这个有效
heart_rate_hourly = heart_rate_seconds.loc[heart_rate_seconds['time'].dt.second() == 0
and heart_rate_seconds['time'].dt.minute() == 0]
出现错误:TypeError: 'Series' object is not callable
heart_rate_hourly = heart_rate_seconds[heart_rate_seconds['time'].dt.strftime("%M") == 0
and heart_rate_seconds['time'].dt.strftime("%S") == 0]
出现错误:ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
on_the_hour = []
for time in heart_rate_seconds['time']:
if time.second == 0 and time.minute == 0:
on_the_hour.append(time)
on_the_hour[:5]
这个有效
on_the_hour = []
for row in heart_rate_seconds:
time = row[1]
if time.second == 0 and time.minute == 0:
on_the_hour.append(time)
on_the_hour[:5]
这不起作用 - AttributeError:'str' 对象没有属性 'second'
谢谢
您可以使用 dt
accessor 为某些属性创建掩码。例如:
import pandas as pd
df = pd.DataFrame({'time': ['01/03/2021 03:30:00 AM', '2021/02/04 13:00:00'],
'values': [0, 1]})
# note that pandas automatically infers the format correctly:
df['time'] = pd.to_datetime(df['time'])
# a mask where min and sec are zero:
m = (df['time'].dt.minute == 0) & (df['time'].dt.second == 0)
print(df[m])
# time values
# 1 2021-02-04 13:00:00 1
请注意,您不必将条件分配给变量就可以使用它们 - 当然您也可以在 loc
.