如何正确找到 df 中的哪些行具有与 Python 上的时间间隔匹配的时间值? Pandas 相关
How to correctly find which rows from a df have a time value that matches with a time interval on Python? Pandas related
我有以下 df
,其中 其中的每个单元格(索引单元格除外)都是字符串类型:
Time Currency Volatility expected Event
0 02:00 GBP Low Volatility Expected Construction Output (MoM) (Jan)
1 02:00 GBP Low Volatility Expected U.K. Construction Output (YoY) (Jan)
2 02:00 GBP High Volatility Expected GDP (YoY)
3 02:00 GBP High Volatility Expected GDP (MoM)
4 02:00 GBP Low Volatility Expected Index of Services
5 02:00 GBP Low Volatility Expected Industrial Production (YoY) (Jan)
6 02:00 GBP Moderate Volatility Expected Industrial Production (MoM) (Jan)
7 02:00 GBP High Volatility Expected Manufacturing Production (MoM) (Jan)
8 02:00 GBP Low Volatility Expected Manufacturing Production (YoY) (Jan)
9 02:00 GBP High Volatility Expected Monthly GDP 3M/3M Change
10 02:00 GBP Moderate Volatility Expected Trade Balance (Jan)
11 02:00 GBP Moderate Volatility Expected Trade Balance Non-EU (Jan)
12 02:00 EUR Moderate Volatility Expected German CPI (MoM) (Feb)
13 02:00 EUR Low Volatility Expected German CPI (YoY) (Feb)
14 02:00 EUR Low Volatility Expected German HICP (MoM) (Feb)
15 02:00 EUR Low Volatility Expected German HICP (YoY) (Feb)
16 03:00 EUR Moderate Volatility Expected Spanish CPI (YoY) (Feb)
17 03:00 EUR Low Volatility Expected Spanish CPI (MoM) (Feb)
18 03:00 EUR Moderate Volatility Expected Spanish HICP (YoY) (Feb)
19 03:00 EUR Low Volatility Expected Spanish HICP (MoM) (Feb)
20 03:00 CNY Low Volatility Expected Chinese Total Social Financing (Feb)
21 03:01 CNY Low Volatility Expected M2 Money Stock (YoY) (Feb)
22 03:01 CNY Moderate Volatility Expected New Loans (Feb)
23 03:01 CNY Low Volatility Expected Outstanding Loan Growth (YoY) (Feb)
24 04:30 GBP Low Volatility Expected Inflation Expectations
25 05:00 EUR High Volatility Expected EU Leaders Summit
26 05:10 EUR Low Volatility Expected Italian 15-Year BTP Auction
27 05:10 EUR Low Volatility Expected Italian 3-Year BTP Auction
28 05:10 EUR Low Volatility Expected Italian 7-Year BTP Auction
29 06:00 EUR Low Volatility Expected Spanish Consumer Confidence
30 06:30 INR Low Volatility Expected Bank Loan Growth
31 06:30 INR Low Volatility Expected Deposit Growth
32 06:30 INR Low Volatility Expected FX Reserves, USD
33 07:00 INR Low Volatility Expected Cumulative Industrial Production (Jan)
34 07:00 INR Low Volatility Expected Industrial Production (YoY) (Jan)
35 07:00 INR Low Volatility Expected Manufacturing Output (MoM) (Jan)
36 07:00 BRL Moderate Volatility Expected CPI (YoY) (Feb)
37 07:00 BRL Moderate Volatility Expected CPI (MoM) (Feb)
38 08:06 BRL Moderate Volatility Expected Brazilian IPCA Inflation Index SA (MoM) (Feb)
39 08:30 CAD Low Volatility Expected Capacity Utilization Rate (Q4)
40 08:30 CAD High Volatility Expected Employment Change (Feb)
41 08:30 CAD Low Volatility Expected Full Employment Change (Feb)
42 08:30 CAD Low Volatility Expected Part Time Employment Change (Feb)
43 08:30 CAD Low Volatility Expected Participation Rate (Feb)
44 08:30 CAD Moderate Volatility Expected Unemployment Rate (Feb)
45 10:00 USD Low Volatility Expected Michigan 5-Year Inflation Expectations (Mar)
从那个 df
开始,我只对与此时间间隔(24 小时)匹配的那些行(如果有的话)感兴趣:
- 开始于 04:00
- 结束于 08:59
由于列 Time
中的每个单元格都包含字符串值,我创建了以下函数以将这些值中的任何一个转换为 datetime.time
对象:
import datetime
def convert_string_to_time(str):
if len(str) < 5 and len(str) > 3:
return datetime.time(hour=int(str[0]), minute=int(str[2:4]))
elif len(str) == 5:
return datetime.time(hour=int(str[0:2]), minute=int(str[3:5]))
else:
return 'not a valid string time'
Example of use:
time1 = '04:35'
timestamp1 = convert_string_to_time(time1)
print(type(timestamp1))
print(timestamp1)
Output:
<class 'datetime.time'>
04:35:00
但现在我卡在了需要使用上面的函数创建以下输出并将其保存在 sub_df
:
的部分
Time Currency Volatility expected Event
24 04:30 GBP Low Volatility Expected Inflation Expectations
25 05:00 EUR High Volatility Expected EU Leaders Summit
26 05:10 EUR Low Volatility Expected Italian 15-Year BTP Auction
27 05:10 EUR Low Volatility Expected Italian 3-Year BTP Auction
28 05:10 EUR Low Volatility Expected Italian 7-Year BTP Auction
29 06:00 EUR Low Volatility Expected Spanish Consumer Confidence
30 06:30 INR Low Volatility Expected Bank Loan Growth
31 06:30 INR Low Volatility Expected Deposit Growth
32 06:30 INR Low Volatility Expected FX Reserves, USD
33 07:00 INR Low Volatility Expected Cumulative Industrial Production (Jan)
34 07:00 INR Low Volatility Expected Industrial Production (YoY) (Jan)
35 07:00 INR Low Volatility Expected Manufacturing Output (MoM) (Jan)
36 07:00 BRL Moderate Volatility Expected CPI (YoY) (Feb)
37 07:00 BRL Moderate Volatility Expected CPI (MoM) (Feb)
38 08:06 BRL Moderate Volatility Expected Brazilian IPCA Inflation Index SA (MoM)(Feb)
39 08:30 CAD Low Volatility Expected Capacity Utilization Rate (Q4)
40 08:30 CAD High Volatility Expected Employment Change (Feb)
41 08:30 CAD Low Volatility Expected Full Employment Change (Feb)
42 08:30 CAD Low Volatility Expected Part Time Employment Change (Feb)
43 08:30 CAD Low Volatility Expected Participation Rate (Feb)
44 08:30 CAD Moderate Volatility Expected Unemployment Rate (Feb)
我不知道如何在 Time
列上垂直迭代以应用 convert_string_to_time(str)
函数来仅获取与所需时间间隔匹配的那些行并将它们存储在新的 df 中叫 sub_df
,我可以在这里得到一些帮助吗?
其实比你想象的要简单。只需使用 pd.to_datetime
将时间转换为 datetime
对象,然后使用 pd.Series.between
mask = pd.to_datetime(df['Time']).between('4:30', '8:59')
filtered = df[mask]
输出:
>>> filtered
Time Currency Volatility expected Event
24 04:30 GBP Low Volatility Expected Inflation Expectations
25 05:00 EUR High Volatility Expected EU Leaders Summit
26 05:10 EUR Low Volatility Expected Italian 15-Year BTP Auction
27 05:10 EUR Low Volatility Expected Italian 3-Year BTP Auction
28 05:10 EUR Low Volatility Expected Italian 7-Year BTP Auction
29 06:00 EUR Low Volatility Expected Spanish Consumer Confidence
30 06:30 INR Low Volatility Expected Bank Loan Growth
31 06:30 INR Low Volatility Expected Deposit Growth
32 06:30 INR Low Volatility Expected FX Reserves, USD
33 07:00 INR Low Volatility Expected Cumulative Industrial Production (Jan)
34 07:00 INR Low Volatility Expected Industrial Production (YoY) (Jan)
35 07:00 INR Low Volatility Expected Manufacturing Output (MoM) (Jan)
36 07:00 BRL Moderate Volatility Expected CPI (YoY) (Feb)
37 07:00 BRL Moderate Volatility Expected CPI (MoM) (Feb)
38 08:06 BRL Moderate Volatility Expected Brazilian IPCA Inflation Index SA (MoM) (Feb)
39 08:30 CAD Low Volatility Expected Capacity Utilization Rate (Q4)
40 08:30 CAD High Volatility Expected Employment Change (Feb)
41 08:30 CAD Low Volatility Expected Full Employment Change (Feb)
42 08:30 CAD Low Volatility Expected Part Time Employment Change (Feb)
43 08:30 CAD Low Volatility Expected Participation Rate (Feb)
44 08:30 CAD Moderate Volatility Expected Unemployment Rate (Feb)
我有以下 df
,其中 其中的每个单元格(索引单元格除外)都是字符串类型:
Time Currency Volatility expected Event
0 02:00 GBP Low Volatility Expected Construction Output (MoM) (Jan)
1 02:00 GBP Low Volatility Expected U.K. Construction Output (YoY) (Jan)
2 02:00 GBP High Volatility Expected GDP (YoY)
3 02:00 GBP High Volatility Expected GDP (MoM)
4 02:00 GBP Low Volatility Expected Index of Services
5 02:00 GBP Low Volatility Expected Industrial Production (YoY) (Jan)
6 02:00 GBP Moderate Volatility Expected Industrial Production (MoM) (Jan)
7 02:00 GBP High Volatility Expected Manufacturing Production (MoM) (Jan)
8 02:00 GBP Low Volatility Expected Manufacturing Production (YoY) (Jan)
9 02:00 GBP High Volatility Expected Monthly GDP 3M/3M Change
10 02:00 GBP Moderate Volatility Expected Trade Balance (Jan)
11 02:00 GBP Moderate Volatility Expected Trade Balance Non-EU (Jan)
12 02:00 EUR Moderate Volatility Expected German CPI (MoM) (Feb)
13 02:00 EUR Low Volatility Expected German CPI (YoY) (Feb)
14 02:00 EUR Low Volatility Expected German HICP (MoM) (Feb)
15 02:00 EUR Low Volatility Expected German HICP (YoY) (Feb)
16 03:00 EUR Moderate Volatility Expected Spanish CPI (YoY) (Feb)
17 03:00 EUR Low Volatility Expected Spanish CPI (MoM) (Feb)
18 03:00 EUR Moderate Volatility Expected Spanish HICP (YoY) (Feb)
19 03:00 EUR Low Volatility Expected Spanish HICP (MoM) (Feb)
20 03:00 CNY Low Volatility Expected Chinese Total Social Financing (Feb)
21 03:01 CNY Low Volatility Expected M2 Money Stock (YoY) (Feb)
22 03:01 CNY Moderate Volatility Expected New Loans (Feb)
23 03:01 CNY Low Volatility Expected Outstanding Loan Growth (YoY) (Feb)
24 04:30 GBP Low Volatility Expected Inflation Expectations
25 05:00 EUR High Volatility Expected EU Leaders Summit
26 05:10 EUR Low Volatility Expected Italian 15-Year BTP Auction
27 05:10 EUR Low Volatility Expected Italian 3-Year BTP Auction
28 05:10 EUR Low Volatility Expected Italian 7-Year BTP Auction
29 06:00 EUR Low Volatility Expected Spanish Consumer Confidence
30 06:30 INR Low Volatility Expected Bank Loan Growth
31 06:30 INR Low Volatility Expected Deposit Growth
32 06:30 INR Low Volatility Expected FX Reserves, USD
33 07:00 INR Low Volatility Expected Cumulative Industrial Production (Jan)
34 07:00 INR Low Volatility Expected Industrial Production (YoY) (Jan)
35 07:00 INR Low Volatility Expected Manufacturing Output (MoM) (Jan)
36 07:00 BRL Moderate Volatility Expected CPI (YoY) (Feb)
37 07:00 BRL Moderate Volatility Expected CPI (MoM) (Feb)
38 08:06 BRL Moderate Volatility Expected Brazilian IPCA Inflation Index SA (MoM) (Feb)
39 08:30 CAD Low Volatility Expected Capacity Utilization Rate (Q4)
40 08:30 CAD High Volatility Expected Employment Change (Feb)
41 08:30 CAD Low Volatility Expected Full Employment Change (Feb)
42 08:30 CAD Low Volatility Expected Part Time Employment Change (Feb)
43 08:30 CAD Low Volatility Expected Participation Rate (Feb)
44 08:30 CAD Moderate Volatility Expected Unemployment Rate (Feb)
45 10:00 USD Low Volatility Expected Michigan 5-Year Inflation Expectations (Mar)
从那个 df
开始,我只对与此时间间隔(24 小时)匹配的那些行(如果有的话)感兴趣:
- 开始于 04:00
- 结束于 08:59
由于列 Time
中的每个单元格都包含字符串值,我创建了以下函数以将这些值中的任何一个转换为 datetime.time
对象:
import datetime
def convert_string_to_time(str):
if len(str) < 5 and len(str) > 3:
return datetime.time(hour=int(str[0]), minute=int(str[2:4]))
elif len(str) == 5:
return datetime.time(hour=int(str[0:2]), minute=int(str[3:5]))
else:
return 'not a valid string time'
Example of use:
time1 = '04:35'
timestamp1 = convert_string_to_time(time1)
print(type(timestamp1))
print(timestamp1)
Output:
<class 'datetime.time'>
04:35:00
但现在我卡在了需要使用上面的函数创建以下输出并将其保存在 sub_df
:
Time Currency Volatility expected Event
24 04:30 GBP Low Volatility Expected Inflation Expectations
25 05:00 EUR High Volatility Expected EU Leaders Summit
26 05:10 EUR Low Volatility Expected Italian 15-Year BTP Auction
27 05:10 EUR Low Volatility Expected Italian 3-Year BTP Auction
28 05:10 EUR Low Volatility Expected Italian 7-Year BTP Auction
29 06:00 EUR Low Volatility Expected Spanish Consumer Confidence
30 06:30 INR Low Volatility Expected Bank Loan Growth
31 06:30 INR Low Volatility Expected Deposit Growth
32 06:30 INR Low Volatility Expected FX Reserves, USD
33 07:00 INR Low Volatility Expected Cumulative Industrial Production (Jan)
34 07:00 INR Low Volatility Expected Industrial Production (YoY) (Jan)
35 07:00 INR Low Volatility Expected Manufacturing Output (MoM) (Jan)
36 07:00 BRL Moderate Volatility Expected CPI (YoY) (Feb)
37 07:00 BRL Moderate Volatility Expected CPI (MoM) (Feb)
38 08:06 BRL Moderate Volatility Expected Brazilian IPCA Inflation Index SA (MoM)(Feb)
39 08:30 CAD Low Volatility Expected Capacity Utilization Rate (Q4)
40 08:30 CAD High Volatility Expected Employment Change (Feb)
41 08:30 CAD Low Volatility Expected Full Employment Change (Feb)
42 08:30 CAD Low Volatility Expected Part Time Employment Change (Feb)
43 08:30 CAD Low Volatility Expected Participation Rate (Feb)
44 08:30 CAD Moderate Volatility Expected Unemployment Rate (Feb)
我不知道如何在 Time
列上垂直迭代以应用 convert_string_to_time(str)
函数来仅获取与所需时间间隔匹配的那些行并将它们存储在新的 df 中叫 sub_df
,我可以在这里得到一些帮助吗?
其实比你想象的要简单。只需使用 pd.to_datetime
将时间转换为 datetime
对象,然后使用 pd.Series.between
mask = pd.to_datetime(df['Time']).between('4:30', '8:59')
filtered = df[mask]
输出:
>>> filtered
Time Currency Volatility expected Event
24 04:30 GBP Low Volatility Expected Inflation Expectations
25 05:00 EUR High Volatility Expected EU Leaders Summit
26 05:10 EUR Low Volatility Expected Italian 15-Year BTP Auction
27 05:10 EUR Low Volatility Expected Italian 3-Year BTP Auction
28 05:10 EUR Low Volatility Expected Italian 7-Year BTP Auction
29 06:00 EUR Low Volatility Expected Spanish Consumer Confidence
30 06:30 INR Low Volatility Expected Bank Loan Growth
31 06:30 INR Low Volatility Expected Deposit Growth
32 06:30 INR Low Volatility Expected FX Reserves, USD
33 07:00 INR Low Volatility Expected Cumulative Industrial Production (Jan)
34 07:00 INR Low Volatility Expected Industrial Production (YoY) (Jan)
35 07:00 INR Low Volatility Expected Manufacturing Output (MoM) (Jan)
36 07:00 BRL Moderate Volatility Expected CPI (YoY) (Feb)
37 07:00 BRL Moderate Volatility Expected CPI (MoM) (Feb)
38 08:06 BRL Moderate Volatility Expected Brazilian IPCA Inflation Index SA (MoM) (Feb)
39 08:30 CAD Low Volatility Expected Capacity Utilization Rate (Q4)
40 08:30 CAD High Volatility Expected Employment Change (Feb)
41 08:30 CAD Low Volatility Expected Full Employment Change (Feb)
42 08:30 CAD Low Volatility Expected Part Time Employment Change (Feb)
43 08:30 CAD Low Volatility Expected Participation Rate (Feb)
44 08:30 CAD Moderate Volatility Expected Unemployment Rate (Feb)