Re: TIME IN XARRAY
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.0', '1979-02-01T00:00:00.0', '1979-03-01T00:00:00.0', ..., '2021-01-01T00:00:00.0', '2021-02-01T00:00:00.0', '2021-03-01T00:00:00.0'], 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.0" 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.0" dtmDate=datetime.strptime(Event,"%Y-%m-%dT%H:%M:%S.0") 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: TIME IN XARRAY
On 15/04/2021 20:20, Chris Angelico wrote: On Fri, Apr 16, 2021 at 4:16 AM Jorge Conforte 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.0', '1979-02-01T00:00:00.0', '1979-03-01T00:00:00.0', ..., '2021-01-01T00:00:00.0', '2021-02-01T00:00:00.0', '2021-03-01T00:00:00.0'], 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")) -- https://mail.python.org/mailman/listinfo/python-list
Re: TIME IN XARRAY
On Fri, Apr 16, 2021 at 4:16 AM Jorge Conforte 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.0', '1979-02-01T00:00:00.0', > '1979-03-01T00:00:00.0', ..., > '2021-01-01T00:00:00.0', > '2021-02-01T00:00:00.0', '2021-03-01T00:00:00.0'], >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(). ChrisA -- https://mail.python.org/mailman/listinfo/python-list
TIME IN XARRAY
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.0', '1979-02-01T00:00:00.0', '1979-03-01T00:00:00.0', ..., '2021-01-01T00:00:00.0', '2021-02-01T00:00:00.0', '2021-03-01T00:00:00.0'], dtype='datetime64[ns]') Please, how can I get the years and months values from this array. Thanks, Conrado -- https://mail.python.org/mailman/listinfo/python-list