Pandas: Change day

前端 未结 2 1391
轮回少年
轮回少年 2020-12-10 02:36

I have a series in datetime format, and need to change the day to 1 for each entry. I have thought of numerous simple solutions, but none of them w

2条回答
  •  情深已故
    2020-12-10 02:47

    You can use .apply and datetime.replace, eg:

    import pandas as pd
    from datetime import datetime
    
    ps = pd.Series([datetime(2014, 1, 7), datetime(2014, 3, 13), datetime(2014, 6, 12)])
    new = ps.apply(lambda dt: dt.replace(day=1))
    

    Gives:

    0   2014-01-01
    1   2014-03-01
    2   2014-06-01
    dtype: datetime64[ns]
    

提交回复
热议问题