python获取一个月之前日期_在Python的日期列表中获取每个月的最后日期

1586010002-jmsa.png

I'm using Python 2.7, PyCharm and Anaconda,

I have a list of dates and I'd like to retrieve the last date of each month present in the array.

Are there any functions or libraries that could help me to do this?

I read the dates from a CSV file and stored them as datetime.

I have the following code:

Dates=[]

Dates1=[]

for date in dates:

temp=xlrd.xldate_as_tuple(int(date),0)

Dates1.append(datetime.datetime(temp[0],temp[1],temp[2]))

for date in Dates1:

if not (dateendDate):

Dates.append(date)

To make it clear, suppose I have:

Dates = [2015-01-20, 2015-01-15, 2015-01-17, 2015-02-21, 2015-02-06]

(Consider it being in datetime format.)

The list I'd like to retrieve is:

[2015-01-20, 2015-02-21]

So far I've googled around, especially in Stack Overflow, but I could only find answers to how I could get the last date of each month, but not from a user-specified list.

解决方案

Pandas can handle this task really well. Load your csv to a dataframe, then run a group by the month and find the max date using the aggregate function:

import pandas as pd

import numpy as np

df = pd.read_csv('/path/to/file/') # Load a dataframe with your file

df.index = df['my_date_field'] # set the dataframe index with your date

dfg = df.groupby(pd.TimeGrouper(freq='M')) # group by month / alternatively use MS for Month Start / referencing the previously created object

# Finally, find the max date in each month

dfg.agg({'my_date_field': np.max})

# To specifically coerce the results of the groupby to a list:

dfg.agg({'my_date_field': np.max})['my_date_field'].tolist()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值