4.2. Series Create

4.2.1. SetUp

>>> import pandas as pd
>>> import numpy as np

4.2.2. From Python sequence

  • list

  • tuple

  • set

  • frozenset

>>> pd.Series([1, 2, 3, 4])
0    1
1    2
2    3
3    4
dtype: int64
>>> pd.Series([1., 2., 3., 4.])
0    1.0
1    2.0
2    3.0
3    4.0
dtype: float64
>>> pd.Series([1, 2, None, 4])
0    1.0
1    2.0
2    NaN
3    4.0
dtype: float64
>>> pd.Series(['a', 'b', 'c', 'd'])
0    a
1    b
2    c
3    d
dtype: object
>>> list('abcd')
['a', 'b', 'c', 'd']
>>>
>>> pd.Series(list('abcd'))
0    a
1    b
2    c
3    d
dtype: object

4.2.3. From Python range

>>> pd.Series(range(4))
0    0
1    1
2    2
3    3
dtype: int64

4.2.4. From Numpy ndarray

>>> pd.Series(np.arange(4.0))
0    0.0
1    1.0
2    2.0
3    3.0
dtype: float64

4.2.5. From Date Range

  • From pd.Timestamp

  • From pd.date_range()

  • More information in Date and Time Types

>>> pd.Series(pd.date_range(start='1969-07-16', end='1969-07-24'))
0   1969-07-16
1   1969-07-17
2   1969-07-18
3   1969-07-19
4   1969-07-20
5   1969-07-21
6   1969-07-22
7   1969-07-23
8   1969-07-24
dtype: datetime64[ns]

4.2.6. Length

>>> s = pd.Series([1, 2, 3, 4])
>>>
>>> len(s)
4

4.2.7. 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 Create Float
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3

# %% English
# 1. Create `result: pd.Series` with 5 float numbers
# 2. One of those values must be `None`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Stwórz `result: pd.Series` z 5 liczbami zmiennoprzecinkowymi
# 2. Jedną z tych wartości musi być `None`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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.dtype
dtype('float64')

>>> assert result.size == 5, \
'Must have 5 elements'

>>> assert result.isnull().any(), \
'At least one value must be `None`'
"""

import pandas as pd


# type: pd.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 Create Even
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3

# %% English
# 1. Create `result: pd.Series` with 10 even numbers
# 2. Run doctests - all must succeed

# %% Polish
# 1. Stwórz `result: pd.Series` z 10 liczbami parzystymi
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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
0     0
1     2
2     4
3     6
4     8
5    10
6    12
7    14
8    16
9    18
dtype: int64
"""

import pandas as pd
import numpy as np


# type: pd.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 Create Randint
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3

# %% English
# 1. Set random seed to zero
# 2. Create `result: pd.Series` with 10 random digits (`int` from `0` to `9`)
# 3. Run doctests - all must succeed

# %% Polish
# 1. Ustaw ziarno losowości na zero
# 2. Stwórz `result: pd.Series` z 10 losowymi cyframi  (`int` from `0` to `9`)
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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
0    5
1    0
2    3
3    3
4    7
5    9
6    3
7    5
8    2
9    4
dtype: int64
"""

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


# type: pd.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 Create Dates
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3

# %% English
# 1. Gagarin flown to space on 1961-04-12
# 2. Armstrong set foot on the Moon on 1969-07-21
# 3. Create `result: pd.Series` with days between Gagarin's launch and Armstrong's first step
# 4. How many days passed?
# 5. Run doctests - all must succeed

# %% Polish
# 1. Gagarin poleciał w kosmos w 1961-04-12
# 2. Armstrong postawił stopę na Księżycu w 1969-07-21
# 3. Stwórz `result: pd.Series` z dniami pomiędzy startem Gagarina a pierwszym krokiem Armstronga
# 4. Jak wiele dni upłynęło?
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> pd.set_option('display.width', 500)
>>> pd.set_option('display.max_columns', 10)
>>> pd.set_option('display.max_rows', 10)

>>> 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  # doctest: +NORMALIZE_WHITESPACE
0      1961-04-12
1      1961-04-13
2      1961-04-14
3      1961-04-15
4      1961-04-16
          ...
3018   1969-07-17
3019   1969-07-18
3020   1969-07-19
3021   1969-07-20
3022   1969-07-21
Length: 3023, dtype: datetime64[ns]
"""

import pandas as pd


# type: pd.Series
result = ...