Pandas temporary function between_datetime?

I used the between_time method for TimeSeries in pandas, which returns all the values โ€‹โ€‹between the specified moments, regardless of their date.

But I need to choose a date and time, because my time series structure contains several dates.

One way to solve this, although rather inflexible, is to simply iterate over the values โ€‹โ€‹and delete those that are irrelevant.

Is there a more elegant way to do this?

+4
source share
1 answer

First you can select the dates of interest, and then use between_time . For example, suppose you have a time series of 72 hours:

 import pandas as pd from numpy.random import randn rng = pd.date_range('1/1/2013', periods=72, freq='H') ts = pd.Series(randn(len(rng)), index=rng) 

To choose between 20:00 and 22:00 on January 2 and 3, you can simply:

 ts['2013-01-02':'2013-01-03'].between_time('20:00', '22:00') 

Give you something like this:

 2013-01-02 20:00:00 0.144399 2013-01-02 21:00:00 0.886806 2013-01-02 22:00:00 0.126844 2013-01-03 20:00:00 -0.464741 2013-01-03 21:00:00 1.856746 2013-01-03 22:00:00 -0.286726 
+5
source

Source: https://habr.com/ru/post/1485303/


All Articles