4.6. Series Select

4.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

4.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

4.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

4.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

4.6.6. Assignments

# %% 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

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

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

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

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

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pandas.core.series.Series, \
'Variable `result` has invalid type, should be `pandas.core.series.Series`'

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

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with first element from `DATA`
# type: pandas.core.series.Series
result = ...


# %% 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

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

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

# %% 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ść

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pandas.core.series.Series, \
'Variable `result` has invalid type, should be `pandas.core.series.Series`'

>>> result
2000-01-06   -0.977278
2000-01-07    0.950088
2000-01-08   -0.151357
2000-01-09   -0.103219
2000-01-10    0.410599
Freq: D, dtype: float64
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with last five elements from `DATA`
# type: pandas.core.series.Series
result = ...


# %% 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

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

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

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

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

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pandas.core.series.Series, \
'Variable `result` has invalid type, should be `pandas.core.series.Series`'

>>> result
2000-01-06   -0.977278
2000-01-03    0.978738
2000-01-04    2.240893
dtype: float64
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with three random elements from `DATA`
# type: pandas.core.series.Series
result = ...


# %% 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

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

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

# %% 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ść

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pandas.core.series.Series, \
'Variable `result` has invalid type, should be `pandas.core.series.Series`'

>>> result
2000-01-06   -0.977278
2000-01-03    0.978738
2000-01-04    2.240893
2000-01-05    1.867558
2000-01-02    0.400157
2000-01-01    1.764052
2000-01-10    0.410599
2000-01-09   -0.103219
2000-01-08   -0.151357
2000-01-07    0.950088
dtype: float64
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with 100% of random elements without replacements from `DATA`
# type: pandas.core.series.Series
result = ...


# %% 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

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

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

# %% 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ść

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

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> pd.set_option('display.width', 250)
>>> pd.set_option('display.max_columns', 20)
>>> pd.set_option('display.max_rows', 30)

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is pd.Series, \
'Variable `result` has invalid type, should be `pd.Series`'

>>> result
2000-01-07    0.950088
2000-01-08   -0.151357
2000-01-08   -0.151357
2000-01-09   -0.103219
2000-01-02    0.400157
2000-01-06   -0.977278
2000-01-10    0.410599
2000-01-09   -0.103219
2000-01-10    0.410599
2000-01-05    1.867558
2000-01-04    2.240893
2000-01-01    1.764052
dtype: float64
"""

import pandas as pd
import numpy as np
np.random.seed(0)

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

# Define variable `result` with 125% of random elements with replacement from `DATA`
# type: pandas.core.series.Series
result = ...