TIME IN XARRAY

Peter Otten __peter__ at web.de
Fri Apr 16 08:18:53 EDT 2021


On 15/04/2021 20:20, Chris Angelico wrote:
> On Fri, Apr 16, 2021 at 4:16 AM Jorge Conforte <jorge.conforte at inpe.br> wrote:
>> I'm using xarray to read netcdf data and I had to time in my data the
>> values:
>>
>> xarray.DataArray 'time' (time: 507)>
>> array(['1979-01-01T00:00:00.000000000', '1979-02-01T00:00:00.000000000',
>>          '1979-03-01T00:00:00.000000000', ...,
>> '2021-01-01T00:00:00.000000000',
>>          '2021-02-01T00:00:00.000000000', '2021-03-01T00:00:00.000000000'],
>>         dtype='datetime64[ns]')
>>
>>
>> Please, how can I get the years and months values from this array.
>>
> 
> Looks like your "time" values are ISO-8601 dates, stored as strings.
> You should be able to parse them easily by hand, or hand them to
> datetime.datetime.fromisoformat().

The dtype is a strong hint that there is a numpy or pandas datetime 
object underneath the misleading repr().

Assuming that x is the column array I'd try

[d.year for d in x]

or

x.map(operator.attrgetter("year"))



More information about the Python-list mailing list