5.2. Series Create
5.2.1. SetUp
>>> import pandas as pd
>>> import numpy as np
5.2.2. From Ordered Sequences
listtuple
>>> data = ['Alice', 'Bob', 'Carol']
>>> s = pd.Series(data)
>>>
>>> s
0 Alice
1 Bob
2 Carol
dtype: object
>>> data = ('Alice', 'Bob', 'Carol')
>>> s = pd.Series(data)
>>>
>>> s
0 Alice
1 Bob
2 Carol
dtype: object
5.2.3. From Unordered Sequences
setfrozensetNot possible
>>> 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
5.2.4. From Python range
>>> pd.Series(range(4))
0 0
1 1
2 2
3 3
dtype: int64
5.2.5. From Numpy ndarray
>>> pd.Series(np.arange(4.0))
0 0.0
1 1.0
2 2.0
3 3.0
dtype: float64
5.2.6. From Date Range
From
pd.TimestampFrom
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]
5.2.7. Length
>>> s = pd.Series([1, 2, 3, 4])
>>>
>>> len(s)
4
5.2.8. Assignments
# %% About
# - Name: Series Create Int
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 5, \
'Variable `result` has an invalid length; expected: `5`.'
>>> assert result.dtype.name == 'int64', \
'Series `result` has an invalid dtype; expected: `int64`.'
>>> 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
0 1
1 2
2 3
3 4
4 5
dtype: int64
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = [1, 2, 3, 4, 5]
# %% Result
result = ...
# %% About
# - Name: Series Create Float
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 6, \
'Variable `result` has an invalid length; expected: `6`.'
>>> assert result.dtype.name == 'float64', \
'Series `result` has an invalid dtype; expected: `float64`.'
>>> assert result.isnull().any(), \
'Series `result` has an invalid value; at least one value must be `None`'
>>> 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)
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = [1.1, 2.2, 3.3, 4.4, 5.5, None]
# %% Result
result = ...
# %% About
# - Name: Series Create Bool
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 3, \
'Variable `result` has an invalid length; expected: `3`.'
>>> assert result.dtype.name == 'object', \
'Series `result` has an invalid dtype; expected: `object`.'
>>> assert result.isnull().any(), \
'Series `result` has an invalid value; at least one value must be `None`'
>>> 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)
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = [True, False, None]
# %% Result
result = ...
# %% About
# - Name: Series Create Str
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 6, \
'Variable `result` has an invalid length; expected: `6`.'
>>> assert result.dtype.name == 'object', \
'Series `result` has an invalid dtype; expected: `object`.'
>>> assert result.isnull().any(), \
'Series `result` has an invalid value; at least one value must be `None`'
>>> 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)
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = ['Alice', 'Bob', 'Carol', 'Dave', 'Eve', None]
# %% Result
result = ...
# %% About
# - Name: Series Create Date
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 5, \
'Variable `result` has an invalid length; expected: `5`.'
>>> assert result.dtype.name == 'datetime64[ns]', \
'Series `result` has an invalid dtype; expected: `datetime64[ns]`.'
>>> 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)
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = [
pd.Timestamp('2001-01-01'),
pd.Timestamp('2002-02-02'),
pd.Timestamp('2003-03-03'),
pd.Timestamp('2004-04-04'),
pd.Timestamp('2005-05-05'),
]
# %% Result
result = ...
# %% About
# - Name: Series Create Datetime
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 5, \
'Variable `result` has an invalid length; expected: `5`.'
>>> assert result.dtype.name == 'datetime64[ns]', \
'Series `result` has an invalid dtype; expected: `datetime64[ns]`.'
>>> 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
0 2001-01-01 01:01:01
1 2002-02-02 02:02:02
2 2003-03-03 03:03:03
3 2004-04-04 04:04:04
4 2005-05-05 05:05:05
dtype: datetime64[ns]
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = [
pd.Timestamp('2001-01-01 01:01:01'),
pd.Timestamp('2002-02-02 02:02:02'),
pd.Timestamp('2003-03-03 03:03:03'),
pd.Timestamp('2004-04-04 04:04:04'),
pd.Timestamp('2005-05-05 05:05:05'),
]
# %% Result
result = ...
# %% About
# - Name: Series Create Timezone
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 5, \
'Variable `result` has an invalid length; expected: `5`.'
>>> assert result.dtype.name == 'object', \
'Series `result` has an invalid dtype; expected: `object`.'
>>> 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
0 2001-01-01 01:01:01+00:00
1 2002-02-02 02:02:02+00:00
2 2003-03-03 03:03:03+01:00
3 2004-04-04 04:04:04+02:00
4 2005-05-05 05:05:05+01:00
dtype: object
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = [
pd.Timestamp('2001-01-01 01:01:01', tz='UTC'),
pd.Timestamp('2002-02-02 02:02:02', tz='GMT'),
pd.Timestamp('2003-03-03 03:03:03', tz='Europe/Warsaw'),
pd.Timestamp('2004-04-04 04:04:04', tz='Poland'),
pd.Timestamp('2005-05-05 05:05:05', tz='Etc/GMT-1'),
]
# %% Result
result = ...
# %% About
# - Name: Series Create DateRange
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% 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. Use `DATA` to create `pd.Series`
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Użyj `DATA`, aby stworzyć `pd.Series`
# 2. Zdefiniuj zmienną `result` z rozwiązaniem
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# 0 2000-01-01
# 1 2000-01-02
# 2 2000-01-03
# 3 2000-01-04
# 4 2000-01-05
# dtype: datetime64[ns]
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> 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`.'
>>> assert len(result) == 5, \
'Variable `result` has an invalid length; expected: `5`.'
>>> assert result.dtype.name == 'datetime64[ns]', \
'Series `result` has an invalid dtype; expected: `datetime64[ns]`.'
>>> 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
0 2000-01-01
1 2000-01-02
2 2000-01-03
3 2000-01-04
4 2000-01-05
dtype: datetime64[ns]
"""
# %% 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
# %% Types
result: pd.Series
# %% Data
DATA = pd.date_range(start='2000-01-01', periods=5, freq='D')
# %% Result
result = ...