B.Quaink
B.Quaink

Reputation: 586

Create numpy datetime format from arrays and single values for year, month and day

I have two datasets, a year and a month. Both are in masked arrays. I also have an observation day which is always 15. I would like to create a masked array with datetimes based on these three values.

Simply using:

obs_Date = np.datetime64(year, month, 15) 

Does not seem to be a thing. How would I create a datetime format based on the year and month arrays and the date value I have?

Upvotes: 0

Views: 969

Answers (1)

FObersteiner
FObersteiner

Reputation: 25664

numpy datetime constructs a bit different than Python datetime, e.g. from an ISO-format string like

import numpy.ma as ma

years = ma.array([2020, 2021, 2022], mask=[0,1,0])
months = ma.array([1, 3, 7], mask=[0,1,0])
day=15

# masks must be identical
assert (years.mask == months.mask).all()

dates = ma.array(
    [f'{y}-{m:02d}-{day}' for y, m in zip(years.data, months.data)],
    dtype='datetime64[D]',
    mask=years.mask
    )

# dates
# masked_array(data=[datetime.date(2020, 1, 15), --,
#                    datetime.date(2022, 7, 15)],
#              mask=[False,  True, False],
#        fill_value=numpy.datetime64('NaT'),
#             dtype='datetime64[D]')

Another option using pandas.to_datetime, keeping the masked values as NaT (not-a-time):

import pandas as pd

dates = pd.to_datetime(
            pd.DataFrame({'year': years, 'month': months, 'day': [day]*years.size})
            )

# dates
# 0   2020-01-15
# 1          NaT
# 2   2022-07-15
# dtype: datetime64[ns]

Upvotes: 1

Related Questions