'Numpy datetime64 extract month-day

I have the 3d data set and I want to have only month-date i.e numpy.datetime64('05-09'). Any idea how to do that? The original 3d datasets:

<xarray.DataArray (time: 153, lat: 281, lon: 481)>
array([[[5839.5454, 5839.581 , 5839.4844, ..., 5865.13  , 5865.8213,
         5865.933 ],
        [5838.614 , 5838.59  , 5838.548 , ..., 5864.121 , 5864.8354,
         5865.13  ],

       
       [[5853.7334, 5854.075 , 5854.291 , ..., 5882.3706, 5882.638 ,
         5882.379 ],
        [5853.297 , 5853.583 , 5853.858 , ..., 5882.602 , 5882.878 ,
         5882.687 ],

        ...,

        [5230.2534, 5230.2534, 5230.2534, ..., 5230.2534, 5230.2534,
         5230.2534]]], dtype=float32)
Coordinates:
  * lon      (lon) float64 -80.0 -79.75 -79.5 -79.25 ... 39.25 39.5 39.75 40.0
  * lat      (lat) float64 20.0 20.25 20.5 20.75 21.0 ... 89.25 89.5 89.75 90.0
  * time     (time) datetime64[ns] 2000-05-01 2000-05-02 ... 2000-09-30


Solution 1:[1]

As an option, I can suggest making a data frame from your array. Change the data frame to a string one and then extract the necessary elements. Turn the data into a list. But, it will be a string format. If you convert it back to datetime64, you will get the other thing.

 import numpy
 import pandas as pd

 aaa = [numpy.datetime64('2000-05-01T00:00:00.000000000'),
 numpy.datetime64('2000-05-02T00:00:00.000000000'),
 numpy.datetime64('2000-05-03T00:00:00.000000000'),
 numpy.datetime64('2000-05-04T00:00:00.000000000'),
 numpy.datetime64('2000-05-05T00:00:00.000000000'),
 numpy.datetime64('2000-05-06T00:00:00.000000000'),
 numpy.datetime64('2000-05-07T00:00:00.000000000'),
 numpy.datetime64('2000-05-08T00:00:00.000000000'),
 numpy.datetime64('2000-05-09T00:00:00.000000000')]

 df = pd.DataFrame(aaa, columns=['abc'])
 df['abc'] = df['abc'].astype('str').str[5:10]
 bbb = df['abc'].to_list()

Output

['05-01', '05-02', '05-03', '05-04', '05-05', '05-06', '05-07', '05-08', '12-01']

If you are satisfied with drawing in the library matplotlib. Then in the code I set it to 'formatter' so that only the month and day are displayed on the x-axis.

import matplotlib.pyplot as plt
import matplotlib.dates
import numpy
import pandas as pd

aaa = [numpy.datetime64('2000-05-01T00:00:00.000000000'),
       numpy.datetime64('2000-05-02T00:00:00.000000000'),
       numpy.datetime64('2000-05-03T00:00:00.000000000'),
       numpy.datetime64('2000-05-04T00:00:00.000000000'),
       numpy.datetime64('2000-05-05T00:00:00.000000000'),
       numpy.datetime64('2000-05-06T00:00:00.000000000'),
       numpy.datetime64('2000-05-07T00:00:00.000000000'),
       numpy.datetime64('2000-05-08T00:00:00.000000000'),
       numpy.datetime64('2000-05-09T00:00:00.000000000')]

ttt = [1, 3, 7, 5, 10, 2, 5, 1, 7]

fig, ax = plt.subplots()
ax.plot(aaa, ttt)
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%m.%d"))
fig.autofmt_xdate()
plt.show()

enter image description here

Solution 2:[2]

You could simply access the dt attribute:

import numpy as np
import pandas as pd


first = np.datetime64('2000-05-01T00:00:00.000000000')
data = [first + np.timedelta64(i, 'D') for i in range(10)]
df = pd.DataFrame(data, columns=['full'])

df['Y'] = df['full'].dt.year
df['M'] = df['full'].dt.month
df['D'] = df['full'].dt.day
print(df)
#         full     Y  M   D
# 0 2000-05-01  2000  5   1
# 1 2000-05-02  2000  5   2
# 2 2000-05-03  2000  5   3
# 3 2000-05-04  2000  5   4
# 4 2000-05-05  2000  5   5
# 5 2000-05-06  2000  5   6
# 6 2000-05-07  2000  5   7
# 7 2000-05-08  2000  5   8
# 8 2000-05-09  2000  5   9
# 9 2000-05-10  2000  5  10

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
Solution 2 norok2