查找给定日期,熊猫的季度结束日期

时间:2015-07-14 12:03:49

标签: python pandas

假设我们有一个类似

的表格
list_fragment_layout.mxl

有没有办法根据<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/myImageView" android:background="#00FF00" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="12dp" /> </RelativeLayout> 列中的日期获取实际季度的具体结束日期?例如,我想将table = [[datetime.datetime(2015, 1, 1), 1, 0.5], [datetime.datetime(2015, 1, 27), 1, 0.5], [datetime.datetime(2015, 1, 31), 1, 0.5], [datetime.datetime(2015, 2, 1), 1, 2], [datetime.datetime(2015, 2, 3), 1, 2], [datetime.datetime(2015, 2, 15), 1, 2], [datetime.datetime(2015, 2, 28), 1, 2], [datetime.datetime(2015, 3, 1), 1, 3], [datetime.datetime(2015, 3, 17), 1, 3], [datetime.datetime(2015, 3, 31), 1, 3]] df = pd.DataFrame(table, columns=['Date', 'Id', 'Value']) 列添加到Date,以便

Q_date

我只考虑第一季度的简单性 - 因为我知道它是什么日期。

4 个答案:

答案 0 :(得分:9)

您可以使用pd.tseries.offsets.QuarterEnd()来实现目标。

import pandas as pd
import datetime

# your data
# ================================
table = [[datetime.datetime(2015, 1, 1), 1, 0.5],
         [datetime.datetime(2015, 1, 27), 1, 0.5],
         [datetime.datetime(2015, 1, 31), 1, 0.5],
         [datetime.datetime(2015, 2, 1), 1, 2],
         [datetime.datetime(2015, 2, 3), 1, 2],
         [datetime.datetime(2015, 2, 15), 1, 2],
         [datetime.datetime(2015, 2, 28), 1, 2],
         [datetime.datetime(2015, 3, 1), 1, 3],
         [datetime.datetime(2015, 3, 17), 1, 3],
         [datetime.datetime(2015, 3, 31), 1, 3]]

df = pd.DataFrame(table, columns=['Date', 'Id', 'Value'])

# processing
# ================================
# in case of 2015.03.31, simple QuarterEnd will roll forward to next quarter, so use DateOffset here to make it robust to this
df['Qdate'] = [date - pd.tseries.offsets.DateOffset(days=1) + pd.tseries.offsets.QuarterEnd() for date in  df.Date]

print(df)


        Date  Id  Value      Qdate
0 2015-01-01   1    0.5 2015-03-31
1 2015-01-27   1    0.5 2015-03-31
2 2015-01-31   1    0.5 2015-03-31
3 2015-02-01   1    2.0 2015-03-31
4 2015-02-03   1    2.0 2015-03-31
5 2015-02-15   1    2.0 2015-03-31
6 2015-02-28   1    2.0 2015-03-31
7 2015-03-01   1    3.0 2015-03-31
8 2015-03-17   1    3.0 2015-03-31
9 2015-03-31   1    3.0 2015-03-31

答案 1 :(得分:2)

真的很棒@Jianxun!这是另一种方法:

import calendar

def f(x):
    q = ((x[0].month-1)//3 + 1)*3
    last = calendar.monthrange(x[0].year,q)[1]
    return datetime.date(x[0].year, q, last)

df['QDate'] = df.apply(f,axis=1)


In [24]: df
Out[24]:
        Date  Id  Value       QDate
0 2015-01-01   1    0.5  2015-03-31
1 2015-01-27   1    0.5  2015-03-31
2 2015-01-31   1    0.5  2015-03-31
3 2015-02-01   1    2.0  2015-03-31
4 2015-02-03   1    2.0  2015-03-31
5 2015-02-15   1    2.0  2015-03-31
6 2015-02-28   1    2.0  2015-03-31
7 2015-03-01   1    3.0  2015-03-31
8 2015-03-17   1    3.0  2015-03-31
9 2015-03-31   1    3.0  2015-03-31

答案 2 :(得分:2)

使用searchsorted是另一种选择:

import datetime
import pandas as pd

table = [[datetime.datetime(2015, 1, 1), 1, 0.5],
         [datetime.datetime(2015, 1, 27), 1, 0.5],
         [datetime.datetime(2015, 1, 31), 1, 0.5],
         [datetime.datetime(2015, 2, 1), 1, 2],
         [datetime.datetime(2015, 2, 3), 1, 2],
         [datetime.datetime(2015, 2, 15), 1, 2],
         [datetime.datetime(2015, 2, 28), 1, 2],
         [datetime.datetime(2015, 3, 1), 1, 3],
         [datetime.datetime(2015, 3, 17), 1, 3],
         [datetime.datetime(2015, 3, 31), 1, 3],
         [datetime.datetime(2015, 4, 1), 1, 3],
]

df = pd.DataFrame(table, columns=['Date', 'Id', 'Value'])
quarters = pd.date_range(
    df['Date'].min(), 
    df['Date'].max()+pd.tseries.offsets.QuarterEnd(), freq='Q')
df['Qdate'] = quarters[quarters.searchsorted(df['Date'].values)]
print(df)

产量

         Date  Id  Value      Qdate
0  2015-01-01   1    0.5 2015-03-31
1  2015-01-27   1    0.5 2015-03-31
2  2015-01-31   1    0.5 2015-03-31
3  2015-02-01   1    2.0 2015-03-31
4  2015-02-03   1    2.0 2015-03-31
5  2015-02-15   1    2.0 2015-03-31
6  2015-02-28   1    2.0 2015-03-31
7  2015-03-01   1    3.0 2015-03-31
8  2015-03-17   1    3.0 2015-03-31
9  2015-03-31   1    3.0 2015-03-31
10 2015-04-01   1    3.0 2015-06-30

通过逐行避免计算,对于中等大的DataFrame,使用上述搜索排序可以快几个数量级。

答案 3 :(得分:2)

一种更简单的方法是将日期转换为一个(季度)期间,然后再转换为一个日期,例如:

int cmpfunc(char *a, char *b) /* yes, you can define parameters as char * */
{
    return strlen(a) - strlen(b);
}

请注意,该季度开始时还有int cmpfunc(char *a, char *b) { int la = strlen(a), lb = strlen(b); if (la != lb) return la - lb; /* la == lb, so we must check lexicographycally */ return strcmp(a, b); }