How floor a date to the first date of that month?

后端 未结 8 1165
我寻月下人不归
我寻月下人不归 2021-01-01 17:43

I have a pandas DataFrame with index column = date.

Input:

            value
date    
1986-01-31  22.93
1986-02-28  15.46
相关标签:
8条回答
  • 2021-01-01 17:59

    From August 2019:

    This should work:

    [x.replace(day=1).date() for x in df['date']]
    

    Only requirement is to make sure date is a datetime, which we can guarantee with a call to pd.to_datetime(df['date'])

    0 讨论(0)
  • 2021-01-01 18:02

    You can also use string datetime formating:

    df['month'] = df['date'].dt.strftime('%Y-%m-01')

    0 讨论(0)
  • 2021-01-01 18:03

    there is a pandas issue about the floor problem

    the suggested way is

    import pandas as pd
    pd.to_datetime(df.date).dt.to_period('M').dt.to_timestamp()
    
    0 讨论(0)
  • 2021-01-01 18:03

    You can use timeseries offset MonthBegin

    from pandas.tseries.offsets import MonthBegin
    df['date'] = pd.to_datetime(df['date']) - MonthBegin(1)
    

    Edit: The above solution does not handle the dates which are already floored to the beginning of the month. Here is an alternative solution.

    Here is a dataframe with additional test cases:

                value
    date    
    1986-01-31  22.93
    1986-02-28  15.46
    2018-01-01  20.00
    2018-02-02  25.00
    

    With timedelta method,

    df.index = pd.to_datetime(df.index)
    df.index = df.index - pd.to_timedelta(df.index.day - 1, unit='d')
    
    
                value
    date    
    1986-01-01  22.93
    1986-02-01  15.46
    2018-01-01  20.00
    2018-02-01  25.00
    
    0 讨论(0)
  • 2021-01-01 18:04

    This will do the trick and no imports necessary. Numpy has a dtype datetime64 which by default pandas sets to [ns] as seen by checking the dtype. You can change this to month, which will start on the first of the month by accessing the numpy array and changing the type.

    df.date = pd.to_datetime(df.date.values.astype('datetime64[M]'))
    

    It would be nice if pandas would implement this with their own astype() method but unfortunately you cannot.

    The above works for data as datetime values or strings, if you already have your data as datetime[ns] type you can omit the pd.to_datetime() and just do:

    df.date = df.date.values.astype('datetime64[M]')
    
    0 讨论(0)
  • 2021-01-01 18:08

    Liked Mikhail Venkov answer. Added below code to have the column added as a timestamp value and keep timezone information

        df['month'] = pd.to_datetime(df['timestamp'].dt.strftime('%Y-%m-01')).dt.tz_localize(timezone) 
    

    where timezone = 'America/Los_Angeles' or whatever zone you want

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