5.4. Series Attributes

5.4.1. SetUp

>>> import pandas as pd
>>>
>>>
>>> data = pd.Series(['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory'])
>>>
>>> data
0      Alice
1        Bob
2      Carol
3       Dave
4        Eve
5    Mallory
dtype: str

5.4.2. Size

  • Number of Elements

>>> data.size
6

5.4.3. NDim

  • Number of Dimensions

>>> data.ndim
1

5.4.4. Shape

  • Tuple Representing the Dimensionality

>>> data.shape
(6,)

5.4.5. Index

  • More information in Pandas Series Index

>>> data.index
RangeIndex(start=0, stop=6, step=1)

5.4.6. Values

>>> data.values
<ArrowStringArray>
['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory']
Length: 6, dtype: str

5.4.7. Assignments

# %% About
# - Name: Pandas Series Attributes
# - 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. Define variable `result` with size of `DATA`
# 2. Run doctests - all must succeed

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

# %% Expected
# >>> result
# 6

# %% 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 int, \
'Variable `result` has an invalid type; expected: `int`.'

>>> 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
6
"""

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

# %% Data
DATA = pd.Series(['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory'])

# %% Result
result = ...

# %% About
# - Name: Pandas Series Attributes
# - 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. Define variable `result` with a data type of `DATA`
# 2. Run doctests - all must succeed

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

# %% Expected
# >>> result
# <StringDtype(na_value=nan)>

# %% 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.StringDtype, \
'Variable `result` has an invalid type; expected: `pd.StringDtype`.'

>>> 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
<StringDtype(na_value=nan)>
"""

# %% 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: np.dtype

# %% Data
DATA = pd.Series(['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory'])

# %% Result
result = ...

# %% About
# - Name: Pandas Series Attributes
# - 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. Define variable `result` with index of `DATA`
# 2. Run doctests - all must succeed

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

# %% Expected
# >>> result
# RangeIndex(start=0, stop=6, step=1)

# %% 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.core.indexes.range.RangeIndex, \
'Variable `result` has an invalid type; expected: `pandas.core.indexes.range.RangeIndex`.'

>>> 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
RangeIndex(start=0, stop=6, step=1)
"""

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

# %% Data
DATA = pd.Series(['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory'])

# %% Result
result = ...

# %% About
# - Name: Pandas Series Attributes
# - 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. Define variable `result` with values of `DATA`
# 2. Run doctests - all must succeed

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

# %% Expected
# >>> result
# <ArrowStringArray>
# ['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory']
# Length: 6, dtype: str

# %% 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.arrays.ArrowStringArray, \
'Variable `result` has an invalid type; expected: `pd.arrays.ArrowStringArray`.'

>>> 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
<ArrowStringArray>
['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory']
Length: 6, dtype: str
"""

# %% 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: np.ndarray

# %% Data
DATA = pd.Series(['Alice', 'Bob', 'Carol', 'Dave', 'Eve', 'Mallory'])

# %% Result
result = ...