Build a Series of closes, select by label and by position, and see arithmetic line up two price series on their dates.
A Series is a column of values with a label on each one. The values are a numpy array. The labels are the index, and they travel with the values through every operation you run.
In this lesson I build a Series of closing prices, select from it by label and by position, swap the labels for dates, and add two price series together to see them line up on those dates.
Step 1. Values, and a label on each one
Hand pd.Series a list and it numbers the values for you, 0 upwards.
2026-01-02 NaN
2026-01-05 597.2
2026-01-06 590.4
2026-01-07 600.4
2026-01-08 NaN
2026-01-09 NaN
dtype: float64
Five values plus four values gave six. pandas took the union of the two indexes, added the values that share a date, and put NaN on the three dates where only one stock had a price. NaN means not a number: the sum is unknown there, not zero.
Strip the labels off and the same addition goes wrong. Position 0 of AAPL is 2 January and position 0 of MSFT is 5 January, so numpy pairs prices from different days.
595.4 is AAPL on 2 January plus MSFT on 5 January. The Series knew those dates did not match. The array did not.
Your turn
Build a with closes 100.0, 102.0, 101.0 on 2, 3 and 4 March 2026, and b with closes 50.0 and 51.0 on 3 and 4 March 2026. Add them. How many rows come back, and where is the NaN?
TipShow answer
import pandas as pda = pd.Series([100.0, 102.0, 101.0], index=pd.to_datetime(["2026-03-02", "2026-03-03", "2026-03-04"]))b = pd.Series([50.0, 51.0], index=pd.to_datetime(["2026-03-03", "2026-03-04"]))print(a + b)# -> 2026-03-02 NaN# -> 2026-03-03 152.0# -> 2026-03-04 152.0# -> dtype: float64print(len(a + b)) # -> 3 <- the union of three dates and two dates
Three rows. The NaN sits on 2 March, the one date b does not have.
NoteTwo Series with the same dates
When both indexes match, nothing is dropped and no NaN appears.