'Extracting year from a masked array - python
I have masked array from which I extracted and converted Datetime from a time series presented in terms of seconds from start date into an 'Array of an object'. Using the following in python.
date=num2date(time, units = data.variables['time'].units)
when I print date I get
Out[41]:
masked_array(data=[cftime.DatetimeGregorian(2002, 8, 23, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2003, 9, 3, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2004, 12, 24, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2005, 6, 18, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2006, 9, 14, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2007, 9, 19, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2008, 3, 14, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2009, 8, 16, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2010, 11, 21, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2011, 11, 7, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2012, 2, 27, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2013, 6, 29, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2014, 6, 15, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2015, 3, 7, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2016, 1, 10, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2017, 7, 25, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2018, 1, 6, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2019, 1, 26, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2020, 12, 23, 9, 0, 0, 0, has_year_zero=False),
cftime.DatetimeGregorian(2021, 4, 30, 9, 0, 0, 0, has_year_zero=False)],
mask=False,
fill_value='?',
dtype=object)
Now what I want is to extract from this is the year and separately the month. How would I go about doing this given it is a masked array?
Solution 1:[1]
date[0].year will return the first year
date[0].month will return the first month
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cong Gao |
