A slice selects a range of positional or labeled elements. Its familiar form, start:end, includes the start and stops before the end in ordinary Python sequences. section 1.3
Use the selector that matches the structure and meaning of the data:
- For a Series, s[:5] returns the first five elements, while s[5:] returns everything from position five through the end. The result remains a Series. section 1.3
- Prefer s.iloc for explicit positional selection and s.loc for index-based selection, such as s.loc['Sep':'Jan']. section 1.3
- For a DataFrame, ordinary brackets usually select columns, but a slice such as df['x':'y'] selects rows and includes the endpoint. section 2.0
- For strings stored in a Series, s.str.slice(3, 5) extracts characters at indexes 3 and 4 from every value. section 5.4
Compare two halves of a Series
Given a ten-value Series s, calculate separate averages with s[:5].mean() and s[5:].mean(). With month labels, use s.loc['Sep':'Jan'].mean() and s.loc['Feb':'Jun'].mean(), then subtract the first result from the second to measure improvement. section 1.3
g = np.random.default_rng(0)
months = 'Sep Oct Nov Dec Jan Feb Mar Apr May Jun'.split()
s = Series(g.integers(70, 101, 10),
index=months)
A labeled Series makes the difference between positional slices and index-based slices concrete. section 1.3


