3.1. Series Create

3.1.1. SetUp

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

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

3.1.3. From Python range

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

3.1.4. From Numpy ndarray

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

3.1.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]

3.1.6. Length

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

3.1.7. Assignments

Code 3.43. Solution
"""
* Assignment: Series Create Float
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

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 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    1.1
    1    2.2
    2    NaN
    3    4.4
    4    5.5
    dtype: float64
"""

import pandas as pd


# type: pd.Series
result = ...


Code 3.44. Solution
"""
* Assignment: Series Create Randint
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

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


Code 3.45. Solution
"""
* Assignment: Series Create Even
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

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 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
np.random.seed(0)


# type: pd.Series
result = ...

Code 3.46. Solution
"""
* Assignment: Series Create Dates
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

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

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