从日期系列中删除计数器

时间:2016-03-28 09:04:34

标签: python python-3.x pandas

相当新的Python并且有一个小问题...

我有一些代码需要约会并返回一系列具有前瞻性的工作日。到目前为止都很好。唯一的问题是在输出中我看起来像是与每个日期相关的计数,所以例如我有这个:

0    2001-01-01
1    2001-01-02
2    2001-01-03
3    2001-01-04
4    2001-01-05
5    2001-01-08
6    2001-01-09
7    2001-01-10
8    2001-01-11
9    2001-01-12
10   2001-01-15

当我真的想看时:

2001-01-01
2001-01-02
2001-01-03
2001-01-04
2001-01-05
2001-01-08
2001-01-09
2001-01-10
2001-01-11
2001-01-12
2001-01-15

请参阅下面的代码。非常感谢任何帮助。

    def getBusinessDayCalender(startDate,insampleLength,outsampleLength):

        print('getBusinessDayCalender')

        from pandas.tseries.offsets import CustomBusinessDay
        weekmask_europe = 'Mon Tue Wed Thu Fri'

        bday_europe = CustomBusinessDay(weekmask=weekmask_europe)

        dt = pd.datetime(startDate.year, startDate.month, startDate.day)

        dts = pd.date_range(dt, periods=insampleLength+outsampleLength, freq=bday_europe)

        dates = pd.Series(dts)
        print(dates)
        return dates

2 个答案:

答案 0 :(得分:0)

这是黑客攻击。但它会很好用。

>>> data_list
'0    2001-01-01\n1    2001-01-02\n2    2001-01-03\n3    2001-01-04\n4    2001-01-05\n5    2001-01-08\n6    2001-01-09\n7    2001-01-10\n8    2001-01-11\n9    2001-01-12\n10   2001-01-15'
>>> [data[data.index('-') - 4:] for data in data_list.split('\n')]
['2001-01-01', '2001-01-02', '2001-01-03', '2001-01-04', '2001-01-05', '2001-01-08', '2001-01-09', '2001-01-10', '2001-01-11', '2001-01-12', '2001-01-15']

答案 1 :(得分:0)

所有系列(和数据框)都在Pandas中编入索引,请参阅docs.

In [2]: s = pd.Series([0,3,1])
        s
Out[2]: 0    0
        1    3
        2    1
        dtype: int64

In [3]: s[1]
Out[3]: 3

您的真实数据位于第二列中。