# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''pyUdemy'': conda)'
#     name: python385jvsc74a57bd0423c48e3256333a0e0252f1efeec30283ab6ba0213e3a05c32b0541c29f89950
# ---

# # Introducing Pandas

# Pandas is an open source library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

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


def series_info(series: pd.Series) -> None:
    print(f"ndim: {series.ndim}")
    print(f"shape: {series.shape}")
    print(f"size: {series.size}")
    print(f"dtype: {series.dtype}")
    print(f"values:\n{series}\n")


def df_info(df: pd.DataFrame) -> None:
    print(f"ndim: {df.ndim}")
    print(f"shape: {df.shape}")
    print(f"size: {df.size}")
    print(f"dtype: {df.dtypes}")
    print(f"values:\n{df}\n")


# +
data = pd.Series(data=[0.25, 0.5, 0.75, 1.0], dtype=np.float32)

series_info(data)
# -

print(type(data.values))
print(data.values)

print(type(data.index))
print(data.index)

print(data[1])

# +
slice_data = data[1:3]

series_info(slice_data)
# -

slice_data[1]

# ### pd.Series
#
# Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.).  
# The axis labels are collectively referred to as the index.
#
# The basic method to create a Series is to call:
#
# ```python
# s = pd.Series(data, index=index)
# ```
#
# - an iterable object (list, np.ndarray)
# - a scalar value (like 5)

# +
data = pd.Series(
    [0.25, 0.5, 0.75, 1.0],
    index=['a', 'b', 'c', 'd']
)

series_info(data)
# -

print(data['a'])

# +
colors_dict = {
    'Red': 12,
    'Green': 8,
    'Blue': 33,
}
colors_s = pd.Series(
    colors_dict,
    dtype=np.int8
)

series_info(colors_s)
# -

print(colors_s['Green'])

series_info(pd.Series([2, 4, 6]))

# ## pd.DataFrame
#
# DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.
#
# Like Series, DataFrame accepts many different kinds of input:
#
# - Dict of 1D ndarrays, lists, dicts, or Series
# - 2-D numpy.ndarray
# - Structured or record ndarray
# - A Series
# - Another DataFrame

# +
codes_dict = {
    'Red': '#FF0000',
    'Green': '#00FF00',
    'Blue': '#0000FF',
}
codes_s = pd.Series(codes_dict, dtype="string")

series_info(codes_s)

# +
combined_df = pd.DataFrame({
    'count': colors_s,
    'code': codes_s
})

df_info(combined_df)
# -

print(combined_df.index)

print(combined_df.columns)

# +
print(type(combined_df["count"]))

series_info(combined_df["count"])

StarcoderdataJupyterScriptsDedupFiltered