【问题标题】:how to check special time period in time frame如何检查时间范围内的特殊时间段
【发布时间】:2021-06-13 00:24:38
【问题描述】:

我有来自 BTCUSDT 价格的 csv 数据,我想知道如何检查这些年数据中的例如 7 月。或者我如何比较一年中 12 个月的第一周。 这是我的代码:

import pandas as pd
import numpy as np

df = pd.read_csv('Binance_BTCUSDT1.csv', parse_dates=['Date'])
df['Date'] = pd.to_datetime(df['Date'])

df['Date'] = pd.to_datetime(df['Date']).dt.date

df['Date']
0       2021-06-06
1       2021-06-05
2       2021-06-04
3       2021-06-03
4       2021-06-02
           ...    
1386    2017-08-21
1387    2017-08-20
1388    2017-08-19
1389    2017-08-18
1390    2017-08-17
Name: Date, Length: 1391, dtype: object

【问题讨论】:

  • Julys dt.date.month == 7?
  • 如果第一周表示前 7 天,那么 dt.date.day <= 7 ?如果第一周意味着第一个星期一到星期日,那么它可能需要工作 - 您可以检查每个月的第一天以获取 dayname 并计算何时是第一个 Monday。稍后您可能会尝试获得第一周
  • 我的意思是在 5 年内(5*365)有很多每日数据。例如,我想在这些数据中特别比较 7 月的 31 天。或比较 7 月的前 7 天。
  • 如果您使用dt.date.month == 7,那么您将拥有所有七月。如果您将它与dt.date.year 一起使用,那么您可以选择一年中的七月,或者您可以将它与groupby() 一起使用,将每年七月分组为单独的组 - 然后您可以比较组。如果您还使用dt.date.day <= 7,那么您可以获得 7 月的前 7 天。

标签: python pandas dataframe date


【解决方案1】:

所有七月

julys = df[ (df['Date'].dt.month == 7) ]

2016 年 7 月

july_2016 = df[ (df['Date'].dt.month == 7) & (df['Date'].dt.year == 2016)]

所有七月的前 7 天

julys_first_7_days = df[ (df['Date'].dt.month == 7) & (df['Date'].dt.day <= 7) ]

但更有用的可以使用groupby创建群组

julys_first_7_days = df[ (df['Date'].dt.month == 7) & (df['Date'].dt.day <= 7) ]

# split date
df['year']  = df['Date'].dt.year
#df['month'] = df['Date'].dt.month
#df['day']   = df['Date'].dt.day

julys_groups = julys_first_7_days.groupby('year')

您可以单独与每个小组合作

for index, grp in julys_groups:
    print('---', index, '---')
    print(grp)

或者您可以为所有组计算一些值 - 在mean price 中为每个组(每年 7 月)

means = julys_groups['price'].mean()

结果:

year
2016    5.571429
2017    5.571429
2018    6.285714
2019    6.142857
2020    4.285714
2021    4.857143

或者您可以将每年转换为列以逐日比较

julys_groups = julys_first_7_days.groupby('year')
new_df = pd.DataFrame()

for index, grp in julys_groups:
    new_df[index] = grp['price'].reset_index(drop=True)

结果:

   2016  2017  2018  2019  2020  2021
0     7     7     9     6     2     8
1     5     9     8     6     6     1
2     8     5     7     8     2     7
3     5     7     4     4     6     1
4     5     4     5     7     2     7
5     7     1     7     5     7     1
6     2     6     4     7     5     9

随机数据的最小工作代码:

import pandas as pd
import numpy as np

# generate some data
df = pd.DataFrame(pd.date_range('2016-01-01', '2022-01-01', freq='d', closed='left'), columns=['Date'])
df['price'] = np.random.randint(0, 10, size=(len(df),))

# split date
df['year']  = df['Date'].dt.year
df['month'] = df['Date'].dt.month
df['day']   = df['Date'].dt.day

# --- filter ---

julys = df[ (df['Date'].dt.month == 7) ]
print(julys)

july_2016 = df[ (df['Date'].dt.month == 7) & (df['Date'].dt.year == 2016)]
print(july_2016)

julys_first_7_days = df[ (df['Date'].dt.month == 7) & (df['Date'].dt.day <= 7)]
print(julys_first_7_days)

# --- groupby ---

# create groups using `julys_first_7_days`

julys_groups = julys_first_7_days.groupby('year')

for index, grp in julys_groups:
    print('---', index, '---')
    print(grp)

# means for every july in julys_first_7_days

means = julys_groups['price'].mean()
print('--- means ---')
print(means)

# convert groups to columns

julys_groups = julys_first_7_days.groupby('year')
new_df = pd.DataFrame()

for index, grp in julys_groups:
    new_df[index] = grp['price'].reset_index(drop=True)
    
print(new_df)

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2016-05-03
    相关资源
    最近更新 更多