Vectorized year/month/day operations with NumPy datetime64

前端 未结 3 1446
梦如初夏
梦如初夏 2021-01-05 06:06

I would like to create vectors of NumPy datetime64 objects from 1-D vectors of years, months, and days, and also go the reverse direction, that is extracting vectors of year

3条回答
  •  执念已碎
    2021-01-05 06:58

    This can be done with pandas without explicit looping (the code is taken from the pandas documentation):

     df = pd.DataFrame({'year': [2015, 2016],
       ....:                    'month': [2, 3],
       ....:                    'day': [4, 5],
       ....:                    'hour': [2, 3]})
       ....: 
    
    In [32]: pd.to_datetime(df)
    Out[32]: 
    0   2015-02-04 02:00:00
    1   2016-03-05 03:00:00
    dtype: datetime64[ns]
    

    of course you can 'floor' the date times to 'day' and return a numpy array with .values

提交回复
热议问题