Pandas: how to compile annual data on top of each other

I have a series of data indexed by time values ​​(float), and I want to take pieces of a series and draw them on top of each other. So, for example, let's say I have stock prices made about every 10 minutes for 20 weeks, and I want to see a weekly template by building 20 lines of stock prices. So my X axis is one week and I have 20 rows (corresponding to prices for the week).

Update

The index is not a uniformly distributed value and is a floating point. This is something like:

t = np.arange(0,12e-9,12e-9/1000.0)
noise = np.random.randn(1000)/1e12
cn = noise.cumsum()
t_noise = t+cn
y = sin(2*math.pi*36e7*t_noise) + noise
df = DataFrame(y,index=t_noise,columns=["A"])
df.plot(marker='.')
plt.axis([0,0.2e-8,0,1])

Thus, the index is not evenly distributed. I am dealing with voltage and time data from a simulator. I would like to know how to create a time window, T and split df into pieces of T long and stack them on top of each other. Therefore, if the data was 20 * T long, then I would have 20 rows in the same section.

Sorry for the confusion; I used the stock analogy, thinking this might help.

+1
source share
2 answers

Assuming an object as a starting point pandas.TimeSeries, you can group items by ISO week number and ISO weekday s datetime.date.isocalendar(). The next statement, which ignores the ISO year, combines the last sample of each day.

In [95]: daily = ts.groupby(lambda x: x.isocalendar()[1:]).agg(lambda s: s[-1])

In [96]: daily
Out[96]: 
key_0
(1, 1)     63
(1, 2)     91
(1, 3)     73
...
(20, 5)    82
(20, 6)    53
(20, 7)    63
Length: 140

, MultiIndex.

In [97]: daily.index = pandas.MultiIndex.from_tuples(daily.index, names=['W', 'D'])

In [98]: daily
Out[98]: 
W   D
1   1    63
    2    91
    3    73
    4    88
    5    84
    6    95
    7    72
...
20  1    81
    2    53
    3    78
    4    64
    5    82
    6    53
    7    63
Length: 140

- "" MultiIndex, , .

In [102]: dofw = "Mon Tue Wed Thu Fri Sat Sun".split()

In [103]: grid = daily.unstack('D').rename(columns=lambda x: dofw[x-1])

In [104]: grid
Out[104]: 
    Mon  Tue  Wed  Thu  Fri  Sat  Sun
W                                    
1    63   91   73   88   84   95   72
2    66   77   96   72   56   80   66
...
19   56   69   89   69   96   73   80
20   81   53   78   64   82   53   63

, , - - ( , , , , ) plot.

grid.T.plot()
+4

. 5 , -

>>> coke = DataReader('KO', 'yahoo', start=datetime(2012,1,1))

>>> startd=coke.index[0]-timedelta(coke.index[0].isoweekday()-1)

>>> rng = array(DateRange(str(startd), periods=90))

>>> chunk=[]

>>> for i in range(18):

... chunk.append(coke[i*5:(i+1)*5].dropna())

...

0

Source: https://habr.com/ru/post/1584368/


All Articles