在python中从月份-年份生成月份的最后一天

在python中从月份-年份生成月份的最后一天,python,pandas,date,datetime,Python,Pandas,Date,Datetime,将对象转换为,从年月日到该月的最后日期 如何生成一个新列,该列的最后日期是从年月日开始的月份?Month_year是一个对象,我无法在python中将其转换为日期格式。我希望最后一个日期采用日期格式,以便计算与数据中其他可用日期列的差异 数据如下: id month_date id_1 04-2018 id_1 04-2018 id_2 04-2018 id_2 05-2019 期望输出: id month_date last_date id_1 04-2018 30-4-20

将对象转换为,从年月日到该月的最后日期

如何生成一个新列,该列的最后日期是从年月日开始的月份?Month_year是一个对象,我无法在python中将其转换为日期格式。我希望最后一个日期采用日期格式,以便计算与数据中其他可用日期列的差异

数据如下:

id   month_date
id_1 04-2018
id_1 04-2018
id_2 04-2018
id_2 05-2019
期望输出:

id   month_date  last_date
id_1 04-2018    30-4-2018
id_1 04-2018    30-4-2018
id_2 04-2018    30-4-2018
id_2 05-2019    31-5-2019
用于删除时间:

df['last_date'] = df['month_date'].dt.to_timestamp(how='end').dt.floor('d')
print (df)
     id month_date  last_date
0  id_1    2018-04 2018-04-30
1  id_1    2018-04 2018-04-30
2  id_2    2018-04 2018-04-30
3  id_2    2019-05 2019-05-31
另一个解决方案:

df['last_date'] = df['month_date'].dt.to_timestamp() + pd.offsets.MonthEnd()
print (df)
     id month_date  last_date
0  id_1    2018-04 2018-04-30
1  id_1    2018-04 2018-04-30
2  id_2    2018-04 2018-04-30
3  id_2    2019-05 2019-05-31
编辑:

其他解决方案:

df['last_date1'] = pd.PeriodIndex(df['month_date']).to_timestamp(how='end').floor('d')
df['last_date2'] = pd.PeriodIndex(df['month_date']).to_timestamp() + pd.offsets.MonthEnd()
print (df)
     id month_date last_date1 last_date2
0  id_1    2018-04 2018-04-30 2018-04-30
1  id_1    2018-04 2018-04-30 2018-04-30
2  id_2    2018-04 2018-04-30 2018-04-30
3  id_2    2019-05 2019-05-31 2019-05-31
用于删除时间:

df['last_date'] = df['month_date'].dt.to_timestamp(how='end').dt.floor('d')
print (df)
     id month_date  last_date
0  id_1    2018-04 2018-04-30
1  id_1    2018-04 2018-04-30
2  id_2    2018-04 2018-04-30
3  id_2    2019-05 2019-05-31
另一个解决方案:

df['last_date'] = df['month_date'].dt.to_timestamp() + pd.offsets.MonthEnd()
print (df)
     id month_date  last_date
0  id_1    2018-04 2018-04-30
1  id_1    2018-04 2018-04-30
2  id_2    2018-04 2018-04-30
3  id_2    2019-05 2019-05-31
编辑:

其他解决方案:

df['last_date1'] = pd.PeriodIndex(df['month_date']).to_timestamp(how='end').floor('d')
df['last_date2'] = pd.PeriodIndex(df['month_date']).to_timestamp() + pd.offsets.MonthEnd()
print (df)
     id month_date last_date1 last_date2
0  id_1    2018-04 2018-04-30 2018-04-30
1  id_1    2018-04 2018-04-30 2018-04-30
2  id_2    2018-04 2018-04-30 2018-04-30
3  id_2    2019-05 2019-05-31 2019-05-31

请将日期部分添加为月1日。比如2019年1月将是2019年1月1日,然后使用datetime.timedelta,加上一个月,减去一天

请将日期部分添加为月1日。比如2019年1月将是2019年1月1日,然后使用datetime.timedelta,加上一个月,减去一天

使用
MonthEnd

Ex:

from pandas.tseries.offsets import MonthEnd
df = pd.DataFrame({"month_date": ["04-2018", "04-2018", "04-2018", "05-2019"]})
df["month_date"] = pd.to_datetime(df["month_date"], format="%m-%Y")
df["last_date"] = (df["month_date"] + MonthEnd(1)).dt.strftime("%d-%m-%Y")
df["month_date"] = df["month_date"].dt.strftime("%m-%Y")
print(df)
  month_date   last_date
0    04-2018  30-04-2018
1    04-2018  30-04-2018
2    04-2018  30-04-2018
3    05-2019  31-05-2019
输出:

from pandas.tseries.offsets import MonthEnd
df = pd.DataFrame({"month_date": ["04-2018", "04-2018", "04-2018", "05-2019"]})
df["month_date"] = pd.to_datetime(df["month_date"], format="%m-%Y")
df["last_date"] = (df["month_date"] + MonthEnd(1)).dt.strftime("%d-%m-%Y")
df["month_date"] = df["month_date"].dt.strftime("%m-%Y")
print(df)
  month_date   last_date
0    04-2018  30-04-2018
1    04-2018  30-04-2018
2    04-2018  30-04-2018
3    05-2019  31-05-2019

使用
MonthEnd

Ex:

from pandas.tseries.offsets import MonthEnd
df = pd.DataFrame({"month_date": ["04-2018", "04-2018", "04-2018", "05-2019"]})
df["month_date"] = pd.to_datetime(df["month_date"], format="%m-%Y")
df["last_date"] = (df["month_date"] + MonthEnd(1)).dt.strftime("%d-%m-%Y")
df["month_date"] = df["month_date"].dt.strftime("%m-%Y")
print(df)
  month_date   last_date
0    04-2018  30-04-2018
1    04-2018  30-04-2018
2    04-2018  30-04-2018
3    05-2019  31-05-2019
输出:

from pandas.tseries.offsets import MonthEnd
df = pd.DataFrame({"month_date": ["04-2018", "04-2018", "04-2018", "05-2019"]})
df["month_date"] = pd.to_datetime(df["month_date"], format="%m-%Y")
df["last_date"] = (df["month_date"] + MonthEnd(1)).dt.strftime("%d-%m-%Y")
df["month_date"] = df["month_date"].dt.strftime("%m-%Y")
print(df)
  month_date   last_date
0    04-2018  30-04-2018
1    04-2018  30-04-2018
2    04-2018  30-04-2018
3    05-2019  31-05-2019

是否正在使用熊猫?是的,我想在熊猫数据框上进行这些更改。如果我检查了“月\年”列的数据类型,我得到的是对象,无法对使用
pandas
的数据执行任何日期关联计算?是的,我要在其上进行这些更改。如果我检查了数据类型中的“月\年”列,我得到的是对象,无法执行任何与日期相关的计算,当我尝试任何一个代码时,我得到了以下错误-“'pandas.\u libs.tslibs.period.period'>不能转换为datetime”@LalithaSundarIyerS-那么解决方案就更简单了,给他们一些时间。month_date列在时间数据期间('2018-04','M')与格式'%M-%Y'(匹配)不匹配,因此我更改了I以反映%Y-%m@LalithaSundarIyerS-答案已编辑,请检查。只能使用带有datetimelike值的.dt访问器-新错误当我尝试这两个代码时,我收到以下错误-“'pandas._libs.tslibs.period.period'>不能转换为datetime”@LalithaSundarIyerS-那么解决方案更简单,给他们一些时间。month_date列在时间数据周期('2018-04','M')中,与格式'%M-%Y'(匹配)不匹配因此,我更改了我以反映%Y-%m@LalithaSundarIyerS-答案已编辑,请检查。只能使用具有datetimelike值的.dt访问器-新错误'pandas.\u libs.tslibs.period.period'>无法转换为datetime。.我收到此错误'pandas.\u libs.tslibs.period.period'>无法转换为datetime。.我收到此错误