python pandas extract unique dates from time series

前端 未结 3 1366
温柔的废话
温柔的废话 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条回答
  • 2021-02-01 19:06

    Using regex:

    (\d{4}-\d{2}-\d{2})
    

    Run it with re.findall function to get all matches:

    result = re.findall(r"(\d{4}-\d{2}-\d{2})", subject)
    
    0 讨论(0)
  • 2021-02-01 19:12

    Just to give an alternative answer to @DSM, look at this other answer from @Psidom

    It would be something like:

    pd.to_datetime(df['DateTime']).dt.date.unique()
    

    It seems to me that it performs slightly better

    0 讨论(0)
  • 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]: <Timestamp: 2012-10-08 07:12:22>
    

    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)
    
    0 讨论(0)
提交回复
热议问题