pandas Series (1d)

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
pd.Series(["a", "b", "c", "d"])
0    a
1    b
2    c
3    d
dtype: object
a1d = np.arange(17)+0.1
a1d.shape
(17,)
s = pd.Series(a1d)
s
0      0.1
1      1.1
2      2.1
3      3.1
4      4.1
5      5.1
6      6.1
7      7.1
8      8.1
9      9.1
10    10.1
11    11.1
12    12.1
13    13.1
14    14.1
15    15.1
16    16.1
dtype: float64
s.index
RangeIndex(start=0, stop=17, step=1)

Series of dtype

df = pd.read_csv('rates.csv')
df.head()
df.dtypes
Time     object
USD     float64
JPY     float64
BGN     float64
CZK     float64
DKK     float64
GBP     float64
CHF     float64
dtype: object
df = pd.read_csv('rates.csv', parse_dates=['Time'])
df.dtypes
Time    datetime64[ns]
USD            float64
JPY            float64
BGN            float64
CZK            float64
DKK            float64
GBP            float64
CHF            float64
dtype: object
type(df.dtypes)
pandas.core.series.Series
if isinstance(df.dtypes, pd.Series):
    raise ValueError('this is a Series but I was expecting something else')
ValueError: this is a Series but I was expecting something else
df.dtypes.index
Index(['Time', 'USD', 'JPY', 'BGN', 'CZK', 'DKK', 'GBP', 'CHF'], dtype='object')
isinstance(df.dtypes, pd.Series)
True
isinstance(df.columns, pd.Series)
False
isinstance(df.columns, pd.Index)
True
df
Time USD JPY BGN CZK DKK GBP CHF
0 2024-01-17 1.0877 160.65 1.9558 24.755 7.4586 0.85818 0.9406
1 2024-01-16 1.0882 159.64 1.9558 24.710 7.4582 0.86078 0.9361
2 2024-01-15 1.0945 159.67 1.9558 24.714 7.4590 0.86075 0.9351
3 2024-01-12 1.0942 159.17 1.9558 24.689 7.4565 0.85950 0.9350
4 2024-01-11 1.0987 159.71 1.9558 24.659 7.4568 0.86145 0.9338
... ... ... ... ... ... ... ... ...
56 2023-10-26 1.0540 158.48 1.9558 24.714 7.4632 0.87170 0.9466
57 2023-10-25 1.0576 158.55 1.9558 24.693 7.4639 0.87240 0.9474
58 2023-10-24 1.0632 159.26 1.9558 24.659 7.4648 0.87025 0.9501
59 2023-10-23 1.0597 158.91 1.9558 24.645 7.4634 0.87153 0.9461
60 2023-10-20 1.0591 158.80 1.9558 24.704 7.4620 0.87213 0.9442

61 rows × 8 columns

assert isinstance(df['USD'], pd.Series)
df['USD']
0     1.0877
1     1.0882
2     1.0945
3     1.0942
4     1.0987
       ...  
56    1.0540
57    1.0576
58    1.0632
59    1.0597
60    1.0591
Name: USD, Length: 61, dtype: float64
isinstance(df['USD'], pd.Series)
True
assert isinstance(df.USD, pd.Series)
df.USD
0     1.0877
1     1.0882
2     1.0945
3     1.0942
4     1.0987
       ...  
56    1.0540
57    1.0576
58    1.0632
59    1.0597
60    1.0591
Name: USD, Length: 61, dtype: float64
df['USD'] # here I extract the USD column, but I lose information about the dates
0     1.0877
1     1.0882
2     1.0945
3     1.0942
4     1.0987
       ...  
56    1.0540
57    1.0576
58    1.0632
59    1.0597
60    1.0591
Name: USD, Length: 61, dtype: float64
# here is how to fix it and use time is index
df.index = df['Time']
df
Time USD JPY BGN CZK DKK GBP CHF
Time
2024-01-17 2024-01-17 1.0877 160.65 1.9558 24.755 7.4586 0.85818 0.9406
2024-01-16 2024-01-16 1.0882 159.64 1.9558 24.710 7.4582 0.86078 0.9361
2024-01-15 2024-01-15 1.0945 159.67 1.9558 24.714 7.4590 0.86075 0.9351
2024-01-12 2024-01-12 1.0942 159.17 1.9558 24.689 7.4565 0.85950 0.9350
2024-01-11 2024-01-11 1.0987 159.71 1.9558 24.659 7.4568 0.86145 0.9338
... ... ... ... ... ... ... ... ...
2023-10-26 2023-10-26 1.0540 158.48 1.9558 24.714 7.4632 0.87170 0.9466
2023-10-25 2023-10-25 1.0576 158.55 1.9558 24.693 7.4639 0.87240 0.9474
2023-10-24 2023-10-24 1.0632 159.26 1.9558 24.659 7.4648 0.87025 0.9501
2023-10-23 2023-10-23 1.0597 158.91 1.9558 24.645 7.4634 0.87153 0.9461
2023-10-20 2023-10-20 1.0591 158.80 1.9558 24.704 7.4620 0.87213 0.9442

61 rows × 8 columns

# Now when we extract a column, we keep the index
# we keep information about the dates
df.USD
Time
2024-01-17    1.0877
2024-01-16    1.0882
2024-01-15    1.0945
2024-01-12    1.0942
2024-01-11    1.0987
               ...  
2023-10-26    1.0540
2023-10-25    1.0576
2023-10-24    1.0632
2023-10-23    1.0597
2023-10-20    1.0591
Name: USD, Length: 61, dtype: float64