python pandas extract unique dates from time series

前端 未结 3 1368
温柔的废话
温柔的废话 2021-02-01 18:27

I have a DataFrame which contains a lot of intraday data, the DataFrame has several days of data, dates are not continuous.

 2012-10-08 07:12:22            0.0          


        
3条回答
  •  旧时难觅i
    2021-02-01 19:20

    If you have a Series like:

    In [116]: df["Date"]
    Out[116]: 
    0           2012-10-08 07:12:22
    1           2012-10-08 09:14:00
    2           2012-10-08 09:15:00
    3           2012-10-08 09:15:01
    4    2012-10-08 09:15:01.500000
    5           2012-10-08 09:15:02
    6    2012-10-08 09:15:02.500000
    7           2012-10-10 07:19:30
    8           2012-10-10 09:14:00
    9           2012-10-10 09:15:00
    10          2012-10-10 09:15:01
    11   2012-10-10 09:15:01.500000
    12          2012-10-10 09:15:02
    Name: Date
    

    where each object is a Timestamp:

    In [117]: df["Date"][0]
    Out[117]: 
    

    you can get only the date by calling .date():

    In [118]: df["Date"][0].date()
    Out[118]: datetime.date(2012, 10, 8)
    

    and Series have a .unique() method. So you can use map and a lambda:

    In [126]: df["Date"].map(lambda t: t.date()).unique()
    Out[126]: array([2012-10-08, 2012-10-10], dtype=object)
    

    or use the Timestamp.date method:

    In [127]: df["Date"].map(pd.Timestamp.date).unique()
    Out[127]: array([2012-10-08, 2012-10-10], dtype=object)
    

提交回复
热议问题