панды: условно совокупные последовательные ряды - PullRequest
0 голосов
/ 01 июня 2018

У меня есть фрейм данных с последовательным индексом (дата для каждого календарного дня) и опорным вектором, который не содержит каждую дату (только рабочие дни).

Я хочу переиндексировать фрейм данных только по датамв опорном векторе с недостающими данными, агрегируются до последней записи до недостающая-даты-секция (т.е. данные в выходных дней должны быть агрегируются вместе в прошлую пятницу).

1006 * в настоящее время у меня естьреализовал это, зациклив обратный индекс и собрав выходные данные, а затем добавив их позже в цикл. Я спрашиваю, есть ли для этого более эффективный «путь массива».
import pandas as pd
import numpy as np
df = pd.DataFrame({'x': np.arange(10), 'y': np.arange(10)**2},
                  index=pd.date_range(start="2018-01-01", periods=10))
print(df)
ref_dates = pd.date_range(start="2018-01-01", periods=10)
ref_dates = ref_dates[:5].append(ref_dates[7:])  # omit 2018-01-06 and -07

# inefficient approach by reverse-traversing the dates, collecting the data
# and aggregating it together with the first date that's in ref_dates
df.sort_index(ascending=False, inplace=True)
collector = []
for dt in df.index:
    if collector and dt in ref_dates:
        # data from previous iteration was collected -> aggregate it and reset collector
        # first append also the current data
        collector.append(df.loc[dt, :].values)
        collector = np.array(collector)

        # applying aggregation function, here sum as example
        aggregates = np.sum(collector, axis=0)

        # setting the new data
        df.loc[dt,:] = aggregates

        # reset collector
        collector = []

    if dt not in ref_dates:
        collector.append(df.loc[dt, :].values)

df = df.reindex(ref_dates)
print(df)

Дает вывод (первый: исходный фрейм данных, второй: целевой фрейм данных)

            x   y
2018-01-01  0   0
2018-01-02  1   1
2018-01-03  2   4
2018-01-04  3   9
2018-01-05  4  16
2018-01-06  5  25
2018-01-07  6  36
2018-01-08  7  49
2018-01-09  8  64
2018-01-10  9  81
             x   y
2018-01-01   0   0
2018-01-02   1   1
2018-01-03   2   4
2018-01-04   3   9
2018-01-05  15  77   # contains the sum of Jan 5th, 6th and 7th
2018-01-08   7  49 
2018-01-09   8  64
2018-01-10   9  81

1 Ответ

0 голосов
/ 01 июня 2018

Все еще имеет цикл понимания списка, но работает.

import pandas as pd
import numpy as np

# Create dataframe which contains all days
df = pd.DataFrame({'x': np.arange(10), 'y': np.arange(10)**2},
                  index=pd.date_range(start="2018-01-01", periods=10))

# create second dataframe which only contains week-days or whatever dates you need.
ref_dates = [x for x in df.index if x.weekday() < 5]

# Set the index of df to a forward filled version of the ref days
df.index = pd.Series([x if x in ref_dates else float('nan') for x in df.index]).fillna(method='ffill')

# Group by unique dates and sum
df = df.groupby(level=0).sum()

print(df)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...