5.6. Series Select

5.6.1. SetUp

>>> import pandas as pd
>>> import numpy as np
>>> np.random.seed(0)
>>>
>>>
>>> s = pd.Series(
...     data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
...     index = pd.date_range(start='1999-12-28', periods=10))
>>> s
1999-12-28    0
1999-12-29    1
1999-12-30    2
1999-12-31    3
2000-01-01    4
2000-01-02    5
2000-01-03    6
2000-01-04    7
2000-01-05    8
2000-01-06    9
Freq: D, dtype: int64

5.6.3. Tail

>>> s.tail(2)
2000-01-05    8
2000-01-06    9
Freq: D, dtype: int64
>>> s.tail(n=1)
2000-01-06    9
Freq: D, dtype: int64

5.6.4. Sample

n number or fraction random rows with and without repetition:

>>> s.sample()
1999-12-30    2
Freq: D, dtype: int64
>>> s.sample(2)
1999-12-31    3
2000-01-02    5
Freq: 2D, dtype: int64
>>> s.sample(n=2, replace=True)
2000-01-04    7
2000-01-04    7
dtype: int64
>>> s.sample(frac=1/4)
1999-12-30    2
1999-12-31    3
Freq: D, dtype: int64
>>> s.sample(frac=0.5)
2000-01-01    4
1999-12-29    1
2000-01-04    7
2000-01-05    8
1999-12-30    2
dtype: int64

5.6.5. Reset Index

>>> s.sample(frac=1.0).reset_index()
       index  0
0 2000-01-02  5
1 1999-12-30  2
2 2000-01-04  7
3 2000-01-01  4
4 1999-12-29  1
5 1999-12-28  0
6 2000-01-03  6
7 2000-01-05  8
8 2000-01-06  9
9 1999-12-31  3

5.6.6. Assignments

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with first element from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z pierwszym elementem z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-01    1.7641
# Freq: D, dtype: float64

# %% Hints
# - `pd.Series.head(n)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-01    1.7641
Freq: D, dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with last five elements from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z pięcioma ostatnimi elementami z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-06   -0.9773
# 2000-01-07    0.9501
# 2000-01-08   -0.1514
# 2000-01-09   -0.1032
# 2000-01-10    0.4106
# Freq: D, dtype: float64

# %% Hints
# - `pd.Series.tail(n)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-06   -0.9773
2000-01-07    0.9501
2000-01-08   -0.1514
2000-01-09   -0.1032
2000-01-10    0.4106
Freq: D, dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with three random elements from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z trzema losowymi elementami z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-06   -0.9773
# 2000-01-03    0.9787
# 2000-01-04    2.2409
# dtype: float64

# %% Hints
# - `pd.Series.sample(n)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> import pandas

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-06   -0.9773
2000-01-03    0.9787
2000-01-04    2.2409
dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with 100% of random elements without replacements from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z 100% losowych elementów bez powtórzeń z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-06   -0.9773
# 2000-01-03    0.9787
# 2000-01-04    2.2409
# 2000-01-05    1.8676
# 2000-01-02    0.4002
# 2000-01-01    1.7641
# 2000-01-10    0.4106
# 2000-01-09   -0.1032
# 2000-01-08   -0.1514
# 2000-01-07    0.9501
# dtype: float64

# %% Hints
# - `Series.sample(frac)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-06   -0.9773
2000-01-03    0.9787
2000-01-04    2.2409
2000-01-05    1.8676
2000-01-02    0.4002
2000-01-01    1.7641
2000-01-10    0.4106
2000-01-09   -0.1032
2000-01-08   -0.1514
2000-01-07    0.9501
dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...

# %% About
# - Name: Series Sample
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define variable `result` with 125% of random elements with replacement from `DATA`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z 125% losowych elementów z powtórzeniami z `DATA`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 2000-01-07    0.9501
# 2000-01-08   -0.1514
# 2000-01-08   -0.1514
# 2000-01-09   -0.1032
# 2000-01-02    0.4002
# 2000-01-06   -0.9773
# 2000-01-10    0.4106
# 2000-01-09   -0.1032
# 2000-01-10    0.4106
# 2000-01-05    1.8676
# 2000-01-04    2.2409
# 2000-01-01    1.7641
# dtype: float64

# %% Hints
# - `Series.sample(frac, replace)`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is pd.Series, \
'Variable `result` has an invalid type; expected: `pd.Series`.'

>>> pd.set_option('display.max_columns', 50)
>>> pd.set_option('display.max_rows', 200)
>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.memory_usage', 'deep')
>>> pd.set_option('display.precision', 4)

>>> result
2000-01-07    0.9501
2000-01-08   -0.1514
2000-01-08   -0.1514
2000-01-09   -0.1032
2000-01-02    0.4002
2000-01-06   -0.9773
2000-01-10    0.4106
2000-01-09   -0.1032
2000-01-10    0.4106
2000-01-05    1.8676
2000-01-04    2.2409
2000-01-01    1.7641
dtype: float64
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import pandas as pd
import numpy as np

# %% Types
result: pd.Series

# %% Data
np.random.seed(0)

DATA = pd.Series(
    data=np.random.randn(10),
    index=pd.date_range('2000-01-01', freq='D', periods=10))

# %% Result
result = ...