question

Upvotes
Accepted
20 0 0 3

Filtering Python Dataframe by Minutes

Hi Developer Community!

I have filtered my minute time series data frame by specific hours and minutes. The first Loop filters for 11:30 the second for 9:30. Lastly, I concatenated them next to each other.

Is there a way to do the same procedure without using an extra (second) loop? I would like to add more time and hour filters.

Thank you 1662982924131.png

pythontime-seriespandasfilter
1662982924131.png (107.9 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
Accepted
53k 137 44 63

@levente.letenyei

Yes, it is feasible to do it in one loop.

The code looks like this:

for i in range(len(majors_list)):
    temp = df_fixing[i][(df_fixing[i].index.hour==11) & (df_fixing[i].index.minute==30)]
    temp.index = temp.index.date
    df_fixing[i] = df_fixing[i][(df_fixing[i].index.hour==9) & (df_fixing[i].index.minute==30)]
    df_fixing[i].index = df_fixing[i].index.date    
    df_fixing[i] = pd.concat([df_fixing[i], temp], axis=1)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hi @Jirapongse ,

thank you very much for your answer! This way of solution worked completely! Cheers!

Upvote
432 3 2 7

Hi @levente.letenyei ,


One quick solution could simply be adding an OR component (df_fixing[i].index.hour == 9) in your first loop so the final code will look like this:

for i in range(len(majors_list)):
    empty_list.append(df_fixing[i][((df_fixing[i].index.hour==11) | (df_fixing[i].index.hour == 9)) & (df_fixing[i].index.minute == 30)])

If you have noticed I have got rid of empty_list[i].index = empty_list[i].index.date to have the full datetime in the final list. And here is the final output:

empty_list[0]

screen-shot-2022-09-12-at-164942.png

Hope this helps.


Best regards,

Haykaz


icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you ver much for your answer @h.aramyan ,

however I need the filtered minutes/hours data next to each other in new columns just like on the screenshot. Do you think it is feasible with one loop?