TIME IN XARRAY

Gys invalid at invalid.com
Sun Apr 18 17:25:34 EDT 2021


On 4/15/21 7:58 PM, Jorge Conforte wrote:
> 
> 
> Hi,
> 
> 
> 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.
> 
> 
> Thanks,
> 
> 
> Conrado

Hi,
maybe this :

from datetime import datetime
import time
# Convert Event to a string
Event="1979-01-01T00:00:00.000000000"
strDate=time.strftime(Event)
print("Date string ",strDate)
# Get the Year from the string strDate
print("Year ",strDate[0:4])
# Get the month from the string strDate
print("Month ",strDate[5:7])
print()
#
# Convert Event to a datetime object
Event="1979-01-01T00:00:00.000000000"
dtmDate=datetime.strptime(Event,"%Y-%m-%dT%H:%M:%S.000000000")
print("datetime object",dtmDate)
# Get the Year from the datetime object
print("Year ",dtmDate.year)
# Get the month from the datetime object
print("Month ",dtmDate.month)

-hth
Gys



More information about the Python-list mailing list