Get year, month or day from numpy datetime64

前端 未结 10 1109
北恋
北恋 2020-12-01 04:13

I have an array of datetime64 type:

dates = np.datetime64([\'2010-10-17\', \'2011-05-13\', \"2012-01-15\"])

Is there a better way than loop

10条回答
  •  不知归路
    2020-12-01 04:33

    As datetime is not stable in numpy I would use pandas for this:

    In [52]: import pandas as pd
    
    In [53]: dates = pd.DatetimeIndex(['2010-10-17', '2011-05-13', "2012-01-15"])
    
    In [54]: dates.year
    Out[54]: array([2010, 2011, 2012], dtype=int32)
    

    Pandas uses numpy datetime internally, but seems to avoid the shortages, that numpy has up to now.

提交回复
热议问题