30

我编写了一个函数来将 pandas 日期时间日期转换为月末:

import pandas
import numpy
import datetime
from pandas.tseries.offsets import Day, MonthEnd

def get_month_end(d):
    month_end = d - Day() + MonthEnd() 
    if month_end.month == d.month:
        return month_end # 31/March + MonthEnd() returns 30/April
    else:
        print "Something went wrong while converting dates to EOM: " + d + " was converted to " + month_end
        raise

这个功能似乎很慢,我想知道是否有更快的选择?我注意到它很慢的原因是我在具有 50'000 个日期的数据框列上运行它,并且我可以看到自从引入该函数以来代码要慢得多(在我将日期转换为月末之前)。

df = pandas.read_csv(inpath, na_values = nas, converters = {open_date: read_as_date})
df[open_date] = df[open_date].apply(get_month_end)

我不确定这是否相关,但我正在阅读以下日期:

def read_as_date(x):
    return datetime.datetime.strptime(x, fmt)
4

6 回答 6

51

修改后,转换为句点,然后返回时间戳就可以了

In [104]: df = DataFrame(dict(date = [Timestamp('20130101'),Timestamp('20130131'),Timestamp('20130331'),Timestamp('20130330')],value=randn(4))).set_index('date')

In [105]: df
Out[105]: 
               value
date                
2013-01-01 -0.346980
2013-01-31  1.954909
2013-03-31 -0.505037
2013-03-30  2.545073

In [106]: df.index = df.index.to_period('M').to_timestamp('M')

In [107]: df
Out[107]: 
               value
2013-01-31 -0.346980
2013-01-31  1.954909
2013-03-31 -0.505037
2013-03-31  2.545073

请注意,这种类型的转换也可以像这样完成,不过上面会稍微快一些。

In [85]: df.index + pd.offsets.MonthEnd(0) 
Out[85]: DatetimeIndex(['2013-01-31', '2013-01-31', '2013-03-31', '2013-03-31'], dtype='datetime64[ns]', name=u'date', freq=None, tz=None)
于 2013-08-14T14:01:15.460 回答
3
import pandas as pd
import numpy as np
import datetime as dt    

df0['Calendar day'] = pd.to_datetime(df0['Calendar day'], format='%m/%d/%Y')
df0['Calendar day'] = df0['Calendar day'].apply(pd.datetools.normalize_date)    
df0['Month Start Date'] = df0['Calendar day'].dt.to_period('M').apply(lambda r: r.start_time)

这段代码应该可以工作。日历日是一列,其中日期以 %m/%d/%Y 格式给出。例如:2014 年 12 月 28 日是 2014 年 12 月 28 日。输出为 2014-12-01 类 'pandas.tslib.Timestamp' 类型。

于 2016-12-05T14:37:21.607 回答
2

如果日期列是日期时间格式并设置为一个月的开始日期,这将增加一个月的时间:

df['date1']=df['date'] + pd.offsets.MonthEnd(0) 
于 2020-08-11T01:04:45.413 回答
1

您还可以使用 numpy 更快地完成它:

import numpy as np
date_array = np.array(['2013-01-01', '2013-01-15', '2013-01-30']).astype('datetime64[ns]')
month_start_date = date_array.astype('datetime64[M]')
于 2017-07-25T10:42:54.193 回答
1

如果日期不在index但在另一列中(适用于 Pandas 0.25.0):

import pandas as pd
import numpy as np

df = pd.DataFrame(dict(date = [pd.Timestamp('20130101'), 
                               pd.Timestamp('20130201'), 
                               pd.Timestamp('20130301'), 
                               pd.Timestamp('20130401')], 
                       value = np.random.rand(4)))
print(df.to_string())

df.date = df.date.dt.to_period('M').dt.to_timestamp('M')
print(df.to_string())

输出:

    date     value
0 2013-01-01  0.295791
1 2013-02-01  0.278883
2 2013-03-01  0.708943
3 2013-04-01  0.483467

        date     value
0 2013-01-31  0.295791
1 2013-02-28  0.278883
2 2013-03-31  0.708943
3 2013-04-30  0.483467
于 2020-03-29T14:36:38.740 回答
0

您正在寻找的可能是:

df.resample('M').last()

@Jeff 前面所说的另一种方法:

df.index = df.index.to_period('M').to_timestamp('M')

于 2020-10-12T19:36:58.677 回答