How do I join arrays, or repeat them?

Join blocks of returns end to end with np.concatenate, and pair every ticker with every date of a long panel with np.repeat and np.tile.

After this lesson, you can join blocks of returns in order and build a long panel with one row per ticker and date.

How do I join two weeks of returns?

np.concatenate([first_week, second_week]) joins two arrays end to end, in the order of the list, so ten daily returns stay ten returns in date order. + does that for lists, while on two arrays it adds the returns at the same positions, so the first of five values is 0.012 + 0.015, two different Mondays added.

The returns are a made-up stock’s daily returns in decimals over two weeks. NumPy sees only the values, so the blocks need to be in date order without overlap before the join.

The following box adds and joins the two weeks.

import numpy as np
import pandas as pd

first_week = np.array([0.012, -0.004, 0.007, -0.021, 0.003])
second_week = np.array([0.015, -0.009, 0.002, -0.013, 0.006])
print(first_week + second_week)
print(np.concatenate([first_week, second_week]))
[ 0.027 -0.013  0.009 -0.034  0.009]
[ 0.012 -0.004  0.007 -0.021  0.003  0.015 -0.009  0.002 -0.013  0.006]

Collecting daily blocks in a list and joining them once copies each value once, while np.append in a loop copies everything gathered so far on every pass.

How do I give every ticker one row per date?

np.repeat(tickers, 3) repeats each ticker three times in a row, and np.tile(dates, 2) repeats the whole list of dates for each ticker, so together they give one row for each ticker and date, a long panel as in the lesson on long and wide panels.

pd.bdate_range("2026-03-02", periods=3) gives three weekdays. Repeating the dates as well would give one ticker the same date twice and miss another date, which .duplicated(["ticker", "date"]).sum() counts.

The following box builds the panel the right way and the wrong way.

tickers = np.array(["AAA", "BBB"])
dates = pd.bdate_range("2026-03-02", periods=3)
panel = pd.DataFrame({"ticker": np.repeat(tickers, 3), "date": np.tile(dates, 2)})
wrong = pd.DataFrame({"ticker": np.repeat(tickers, 3), "date": np.repeat(dates, 2)})
print(panel)
print(panel.duplicated(["ticker", "date"]).sum(), wrong.duplicated(["ticker", "date"]).sum())
  ticker       date
0    AAA 2026-03-02
1    AAA 2026-03-03
2    AAA 2026-03-04
3    BBB 2026-03-02
4    BBB 2026-03-03
5    BBB 2026-03-04
0 2

The ticker stays fixed within its block, so it is repeated, and the dates cycle within each block, so they are tiled.

Your turn

Build a panel of three tickers, AAA, BBB and CCC, and the five weekdays from 9 March 2026, and print row 10. You should see CCC on 9 March.

AAA fills rows 0 to 4 and BBB rows 5 to 9.

week = pd.bdate_range("2026-03-09", periods=5)
five_day_panel = pd.DataFrame({"ticker": np.repeat(["AAA", "BBB", "CCC"], 5), "date": np.tile(week, 3)})
print(five_day_panel.iloc[10])
ticker                    CCC
date      2026-03-09 00:00:00
Name: 10, dtype: object