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

后端 未结 8 1166
我寻月下人不归
我寻月下人不归 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 18:13
    dt_1 = "2016-02-01"
    def first_day(dt):
        lt_split = dt.split("-")
        return "-".join([lt_split[0], lt_split[1], "01"])
    
    print first_day(dt_1)
    

    For Panda's DataFrame, you can use dt["col_name_date"].apply(first_day).

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

    Here's another 'pandonic' way to do it:

    df.date - pd.Timedelta('1 day') * (df.date.dt.day - 1)
    
    0 讨论(0)
提交回复
热议问题