'Get latest N days/hours/minutes data considering business hours and days with pandas

I have following data

                                 open        high         low       close
Datetime                                                                 
2022-05-12 13:00:00-04:00  141.779999  142.470001  140.000000  140.259995
2022-05-12 14:00:00-04:00  140.220001  140.479996  138.800003  139.600006
2022-05-12 15:00:00-04:00  139.610001  142.860001  139.399994  142.589996
2022-05-13 09:00:00-04:00  144.589996  145.229996  143.250000  144.984406 <--- Friday
2022-05-13 10:00:00-04:00  144.990005  146.919998  144.979996  146.860001
2022-05-13 11:00:00-04:00  146.869995  147.839996  146.559998  147.039093
2022-05-13 12:00:00-04:00  147.059998  148.104996  146.809998  147.255005
2022-05-13 13:00:00-04:00  147.257202  147.479996  146.000000  146.214996
2022-05-13 14:00:00-04:00  146.199997  146.929993  144.800003  146.860001
2022-05-13 15:00:00-04:00  146.878403  147.809998  145.919998  147.130005
2022-05-16 09:00:00-04:00  145.350006  146.160004  144.500000  145.023804 <--- Monday
2022-05-16 10:00:00-04:00  145.020004  146.660004  144.419998  144.429993
2022-05-16 11:00:00-04:00  144.440002  146.940002  144.179993  146.649994
2022-05-16 12:00:00-04:00  146.649994  146.800003  145.229996  145.380005
2022-05-16 13:00:00-04:00  145.369995  146.768997  145.149994  146.304993
2022-05-16 14:00:00-04:00  146.309998  147.519897  146.059998  146.955002
2022-05-16 15:00:00-04:00  146.944794  147.169998  145.000000  145.539993
2022-05-17 09:00:00-04:00  148.860001  149.199997  147.630005  147.797806 <--- Tuesday
2022-05-17 10:00:00-04:00  147.789993  148.320007  146.679993  147.339996
2022-05-17 11:00:00-04:00  147.339996  148.679993  147.050003  147.990601
2022-05-17 12:00:00-04:00  148.000000  148.401794  147.460007  147.990005
2022-05-17 13:00:00-04:00  147.990005  148.830093  147.649994  148.550003
2022-05-17 14:00:00-04:00  148.539993  149.330002  147.300293  149.109894
2022-05-17 15:00:00-04:00  149.106003  149.770004  148.789993  149.250000
2022-05-17 16:00:00-04:00  149.240005  149.240005  149.240005  149.240005 <--- Last recorded date

Based on last record, I want to get data for N time-interval, where N can be in days, hours, minutes, weeks, months. The weekends should be skipped as well as any time outside working hours 9:30 - 16:00.

For Ex., N = 2 days

                                 open        high         low       close
Datetime                                                                 
2022-05-12 13:00:00-04:00  141.779999  142.470001  140.000000  140.259995
2022-05-12 14:00:00-04:00  140.220001  140.479996  138.800003  139.600006
2022-05-12 15:00:00-04:00  139.610001  142.860001  139.399994  142.589996
2022-05-13 09:00:00-04:00  144.589996  145.229996  143.250000  144.984406 <--- Friday
2022-05-13 10:00:00-04:00  144.990005  146.919998  144.979996  146.860001
2022-05-13 11:00:00-04:00  146.869995  147.839996  146.559998  147.039093
2022-05-13 12:00:00-04:00  147.059998  148.104996  146.809998  147.255005
2022-05-13 13:00:00-04:00  147.257202  147.479996  146.000000  146.214996
2022-05-13 14:00:00-04:00  146.199997  146.929993  144.800003  146.860001
2022-05-13 15:00:00-04:00  146.878403  147.809998  145.919998  147.130005 <====Result===
2022-05-16 09:00:00-04:00  145.350006  146.160004  144.500000  145.023804 <--- Monday
2022-05-16 10:00:00-04:00  145.020004  146.660004  144.419998  144.429993
2022-05-16 11:00:00-04:00  144.440002  146.940002  144.179993  146.649994
2022-05-16 12:00:00-04:00  146.649994  146.800003  145.229996  145.380005
2022-05-16 13:00:00-04:00  145.369995  146.768997  145.149994  146.304993
2022-05-16 14:00:00-04:00  146.309998  147.519897  146.059998  146.955002
2022-05-16 15:00:00-04:00  146.944794  147.169998  145.000000  145.539993
2022-05-17 09:00:00-04:00  148.860001  149.199997  147.630005  147.797806 <--- Tuesday
2022-05-17 10:00:00-04:00  147.789993  148.320007  146.679993  147.339996
2022-05-17 11:00:00-04:00  147.339996  148.679993  147.050003  147.990601
2022-05-17 12:00:00-04:00  148.000000  148.401794  147.460007  147.990005
2022-05-17 13:00:00-04:00  147.990005  148.830093  147.649994  148.550003
2022-05-17 14:00:00-04:00  148.539993  149.330002  147.300293  149.109894
2022-05-17 15:00:00-04:00  149.106003  149.770004  148.789993  149.250000
2022-05-17 16:00:00-04:00  149.240005  149.240005  149.240005  149.240005 <--- Last recorded date

N = 10 hours

                                 open        high         low       close
Datetime                                                                 
2022-05-12 13:00:00-04:00  141.779999  142.470001  140.000000  140.259995
2022-05-12 14:00:00-04:00  140.220001  140.479996  138.800003  139.600006
2022-05-12 15:00:00-04:00  139.610001  142.860001  139.399994  142.589996
2022-05-13 09:00:00-04:00  144.589996  145.229996  143.250000  144.984406 <--- Friday
2022-05-13 10:00:00-04:00  144.990005  146.919998  144.979996  146.860001
2022-05-13 11:00:00-04:00  146.869995  147.839996  146.559998  147.039093
2022-05-13 12:00:00-04:00  147.059998  148.104996  146.809998  147.255005
2022-05-13 13:00:00-04:00  147.257202  147.479996  146.000000  146.214996
2022-05-13 14:00:00-04:00  146.199997  146.929993  144.800003  146.860001
2022-05-13 15:00:00-04:00  146.878403  147.809998  145.919998  147.130005
2022-05-16 09:00:00-04:00  145.350006  146.160004  144.500000  145.023804 <--- Monday
2022-05-16 10:00:00-04:00  145.020004  146.660004  144.419998  144.429993
2022-05-16 11:00:00-04:00  144.440002  146.940002  144.179993  146.649994
2022-05-16 12:00:00-04:00  146.649994  146.800003  145.229996  145.380005
2022-05-16 13:00:00-04:00  145.369995  146.768997  145.149994  146.304993 <====Result===
2022-05-16 14:00:00-04:00  146.309998  147.519897  146.059998  146.955002
2022-05-16 15:00:00-04:00  146.944794  147.169998  145.000000  145.539993
2022-05-17 09:00:00-04:00  148.860001  149.199997  147.630005  147.797806 <--- Tuesday
2022-05-17 10:00:00-04:00  147.789993  148.320007  146.679993  147.339996
2022-05-17 11:00:00-04:00  147.339996  148.679993  147.050003  147.990601
2022-05-17 12:00:00-04:00  148.000000  148.401794  147.460007  147.990005
2022-05-17 13:00:00-04:00  147.990005  148.830093  147.649994  148.550003
2022-05-17 14:00:00-04:00  148.539993  149.330002  147.300293  149.109894
2022-05-17 15:00:00-04:00  149.106003  149.770004  148.789993  149.250000
2022-05-17 16:00:00-04:00  149.240005  149.240005  149.240005  149.240005 <--- Last recorded date

How can I retrieve data like above examples for different time intervals. Thanks :)

Note that, last recorded date can be like - 2022-05-17 15:36:00-04:00, so 2 day difference will be 2022-05-13 15:36:00-04:00.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source