Filtering Pandas DataFrames on dates

前端 未结 12 1079
时光取名叫无心
时光取名叫无心 2020-11-22 16:15

I have a Pandas DataFrame with a \'date\' column. Now I need to filter out all rows in the DataFrame that have dates outside of the next two months. Essentially, I only need

相关标签:
12条回答
  • 2020-11-22 16:44

    And if your dates are standardized by importing datetime package, you can simply use:

    df[(df['date']>datetime.date(2016,1,1)) & (df['date']<datetime.date(2016,3,1))]  
    

    For standarding your date string using datetime package, you can use this function:

    import datetime
    datetime.datetime.strptime
    
    0 讨论(0)
  • 2020-11-22 16:47

    If your datetime column have the Pandas datetime type (e.g. datetime64[ns]), for proper filtering you need the pd.Timestamp object, for example:

    from datetime import date
    
    import pandas as pd
    
    value_to_check = pd.Timestamp(date.today().year, 1, 1)
    filter_mask = df['date_column'] < value_to_check
    filtered_df = df[filter_mask]
    
    0 讨论(0)
  • 2020-11-22 16:49

    The shortest way to filter your dataframe by date: Lets suppose your date column is type of datetime64[ns]

    # filter by single day
    df = df[df['date'].dt.strftime('%Y-%m-%d') == '2014-01-01']
    
    # filter by single month
    df = df[df['date'].dt.strftime('%Y-%m') == '2014-01']
    
    # filter by single year
    df = df[df['date'].dt.strftime('%Y') == '2014']
    
    0 讨论(0)
  • 2020-11-22 16:52

    If the dates are in the index then simply:

    df['20160101':'20160301']
    
    0 讨论(0)
  • 2020-11-22 16:56

    I'm not allowed to write any comments yet, so I'll write an answer, if somebody will read all of them and reach this one.

    If the index of the dataset is a datetime and you want to filter that just by (for example) months, you can do following:

    df.loc[df.index.month = 3]
    

    That will filter the dataset for you by March.

    0 讨论(0)
  • 2020-11-22 16:57

    You could just select the time range by doing: df.loc['start_date':'end_date']

    0 讨论(0)
提交回复
热议问题