如何使用python遍历特定一天的时间?

时间:2019-12-04 06:30:57

标签: python pandas dataframe data-science python-datetime

我有以下温度读数的时间序列数据:

DT                Temperature
01/01/2019 0:00     41
01/01/2019 1:00     42
01/01/2019 2:00     44
......
01/01/2019 23:00    41
01/02/2019 0:00     44

我正在尝试编写一个函数,比较给定日期的每小时温度变化。任何大于3的更改都会增加quickChange计数器。像这样:

def countChange(day):
    for dt in day:
        if dt+1 - dt > 3: quickChange = quickChange+1

我可以在一天前调用该函数:countChange(df.loc['2018-01-01'])

2 个答案:

答案 0 :(得分:3)

Series.diff3进行比较,并按True计算sum的值:

np.random.seed(2019)

rng = (pd.date_range('2018-01-01', periods=10, freq='H').tolist() +
      pd.date_range('2018-01-02', periods=10, freq='H').tolist())
df = pd.DataFrame({'Temperature': np.random.randint(100, size=20)}, index=rng)  
print (df)
                     Temperature
2018-01-01 00:00:00           72
2018-01-01 01:00:00           31
2018-01-01 02:00:00           37
2018-01-01 03:00:00           88
2018-01-01 04:00:00           62
2018-01-01 05:00:00           24
2018-01-01 06:00:00           29
2018-01-01 07:00:00           15
2018-01-01 08:00:00           12
2018-01-01 09:00:00           16
2018-01-02 00:00:00           48
2018-01-02 01:00:00           71
2018-01-02 02:00:00           83
2018-01-02 03:00:00           12
2018-01-02 04:00:00           80
2018-01-02 05:00:00           50
2018-01-02 06:00:00           95
2018-01-02 07:00:00            5
2018-01-02 08:00:00           24
2018-01-02 09:00:00           28

#if necessary create DatetimeIndex if DT is column
df = df.set_index("DT")

def countChange(day):
    return (day['Temperature'].diff() > 3).sum()

print (countChange(df.loc['2018-01-01']))
4

print (countChange(df.loc['2018-01-02']))
9

答案 1 :(得分:1)

尝试pandas.DataFrame.diff:

df = pd.DataFrame({'dt': ["01/01/2019 0:00","01/01/2019 1:00","01/01/2019 2:00","01/01/2019 23:00","01/02/2019 0:00"], 
                    'Temperature': [41, 42, 44, 41, 44]})

df = df.sort_values("dt")
df = df.set_index("dt")

def countChange(df):
    df["diff"] = df["Temperature"].diff()
    return df.loc[df["diff"] > 3, "diff"].count()

quickchange = countChange(df.loc["2018-01-01"])