0

I am trying to extract random multiple whole day data from pandas DateTimeIndex series, But it returns only the first hour data when the multiple days are passed as a list.

dt = pd.date_range('11-1-2022','11-4-2022',freq='6H').to_series()

When I want to extract single day it works fine

In [204]: dt['11-3-2022']
Out[204]:
2022-11-03 00:00:00   2022-11-03 00:00:00
2022-11-03 06:00:00   2022-11-03 06:00:00
2022-11-03 12:00:00   2022-11-03 12:00:00
2022-11-03 18:00:00   2022-11-03 18:00:00
Freq: 6H, dtype: datetime64[ns]

But when I want to extract multiple random days it only gives the first hour.

In [205]: dt[['11-1-2022','11-3-2022']]
Out[205]:
2022-11-01   2022-11-01
2022-11-03   2022-11-03
dtype: datetime64[ns]

How do I get all hours in the series for a given list of days.

kbk78
  • 139
  • 4

1 Answers1

0

This is one way you can filter your time series: Assuming you want all times between '11-1-2022' and '11-3-2022' inclusive:

da = dt.where(dt >= pd.to_datetime('11-1-2022')).where(dt <= pd.to_datetime('11-3-2022'))
da.dropna()  

Yields:

2022-11-01 00:00:00    2022-11-01 00:00:00
2022-11-01 06:00:00    2022-11-01 06:00:00
2022-11-01 12:00:00    2022-11-01 12:00:00
2022-11-01 18:00:00    2022-11-01 18:00:00
2022-11-02 00:00:00    2022-11-02 00:00:00
2022-11-02 06:00:00    2022-11-02 06:00:00
2022-11-02 12:00:00    2022-11-02 12:00:00
2022-11-02 18:00:00    2022-11-02 18:00:00
2022-11-03 00:00:00    2022-11-03 00:00:00
2022-11-03 06:00:00    2022-11-03 06:00:00
2022-11-03 12:00:00    2022-11-03 12:00:00
2022-11-03 18:00:00    2022-11-03 18:00:00

If you only want data for specific date then you can do: def selectDates(pdS: pd.Series, itms:list[str]) ->

pd.Series:
    rslt = pdS[itms[0]]
    for i in range(1, len(itms)):
        rslt = pd.concat([rslt, pdS[itms[i]]])
    return rslt  

Then running selectDates(dt, ['11-1-2022','11-3-2022']) will yield:

2022-11-01 00:00:00   2022-11-01 00:00:00
2022-11-01 06:00:00   2022-11-01 06:00:00
2022-11-01 12:00:00   2022-11-01 12:00:00
2022-11-01 18:00:00   2022-11-01 18:00:00
2022-11-03 00:00:00   2022-11-03 00:00:00
2022-11-03 06:00:00   2022-11-03 06:00:00
2022-11-03 12:00:00   2022-11-03 12:00:00
2022-11-03 18:00:00   2022-11-03 18:00:00
itprorh66
  • 3,110
  • 4
  • 9
  • 21
  • Want for specific days in the list only, not "between" the dates. to list all values between the dates I would actually use dt['11-1-2022':'11-3-2022'] – kbk78 Nov 15 '22 at 01:32
  • See edits for how that can be done. – itprorh66 Nov 15 '22 at 13:46
  • was looking for a more pythonic or efficient way to do this, If I had to loop through I could just do: pd.concat([dt[c] for c in ['11-1-2022','11-3-2022']]) – kbk78 Nov 15 '22 at 19:55
  • So what makes you think list comprehension is not pythonic? – itprorh66 Nov 15 '22 at 20:17