0

Assume I have this data set (or a more extensive one):

import numpy as np

array = np.array([
    np.datetime64('2022-01-03'),
    np.datetime64('2022-05-03'),
    np.datetime64('2022-12-03')
])

I want to separate the dates by the month into different arrays. How can I do that?

A related and more general question would be how to filter them by year, month, day...

1 Answers1

0

In case you want them structured in a DataFrame you can do this:

    array = np.array([
        np.datetime64('2022-01-03'),
        np.datetime64('2022-05-03'),
        np.datetime64('2022-12-03')
    ])

dates=pd.DataFrame()
dates["date"]=pd.to_datetime(array)
for i in range(1,13):
    dates[f"month_{i}"]=[x if x.month==i else np.nan for x in list(dates["date"])]    

Output:

        date    month_1  month_2  ...  month_10  month_11   month_12
0 2022-01-03 2022-01-03      NaN  ...       NaN       NaN        NaT
1 2022-05-03        NaT      NaN  ...       NaN       NaN        NaT
2 2022-12-03        NaT      NaN  ...       NaN       NaN 2022-12-03

[3 rows x 13 columns]
  • In the real case, I'm reading a `.xls` file with `pandas`, so that is an easy solution, however, I prefer to use `numpy` when posible since I'm more familiar with it and I like it more than `pandas`. – Abel Gutiérrez Sep 03 '22 at 08:00