Converting between datetime, Timestamp and datetime64

前端 未结 12 1284
傲寒
傲寒 2020-11-22 01:43

How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?

In the following code, I create a datetime,

12条回答
  •  孤街浪徒
    2020-11-22 02:12

    Some solutions work well for me but numpy will deprecate some parameters. The solution that work better for me is to read the date as a pandas datetime and excract explicitly the year, month and day of a pandas object. The following code works for the most common situation.

    def format_dates(dates):
        dt = pd.to_datetime(dates)
        try: return [datetime.date(x.year, x.month, x.day) for x in dt]    
        except TypeError: return datetime.date(dt.year, dt.month, dt.day)
    

提交回复
热议问题