import numpy as np

ar = 0.5 * np.eye(3)
ar[2, 1] = 1
ar

"""
Explanation: Pandas objects
So far, we have manipulated data which were stored in NumPy arrays. Let us consider 2D data.
End of explanation
"""


import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(ar, cmap=plt.cm.gray)

"""
Explanation: We could visualize it with Matplotlib.
End of explanation
"""


import pandas as pd

df = pd.DataFrame(ar)

df

"""
Explanation: Raw data could look like this. Say that columns hold variables and rows hold observations (or records). We may want to label the data (set some metadata). We may also want to handle non-numerical data. Then, we want to store our data in a DataFrame, a 2D labelled data structure with columns of potentially different types.
The DataFrame object
End of explanation
"""


df.size

df.shape

"""
Explanation: The DataFrame object has attributes...
End of explanation
"""


df.columns = ['red', 'green', 'blue']

"""
Explanation: ... and methods, as we shall see in the following. For now, let us label our data.
End of explanation
"""


df

df.plot()

"""
Explanation: Note that, alternatively, you could have done df.rename(columns={0: 'red', 1: 'green', 2: 'blue'}, inplace=True).
End of explanation
"""


df['green']

"""
Explanation: (This is a terrible visualization though... 3-cycle needed!)
Hands-on exercises

Create another DataFrame, df2, equal to df (with the same values for each column) by passing a dictionary to pd.DataFrame(). You can check your answer by running pd.testing.assert_frame_equal(df, df2, check_like=True). 
What is the type of object df[['green']]?
What is the type of object df['green']?

The Series object
A Series is a 1D labelled data structure.
End of explanation
"""


pd.Series(range(10))

s = pd.Series(['first', 'second', 'third'])

s

t = pd.Series([pd.Timestamp('2017-09-01'), pd.Timestamp('2017-09-02'), pd.Timestamp('2017-09-03')])

t

alpha = pd.Series(0.1 * np.arange(1, 4))

alpha.plot(kind='bar')

df['alpha'] = alpha

df

"""
Explanation: It can hold any data type.
End of explanation
"""


alpha.index

df.index

alpha

alpha.index = s

alpha

alpha.index

df.set_index(s)

df.set_index(s, inplace=True)

"""
Explanation: Hands-on exercises

Create a series equal (element-wise) to the product of the 'green' variable and the 'alpha' variable. (Hint: It works like NumPy arrays.)
Label this series as 'pre_multiplied_green'. (Hint: Use tab completion to explore the list of attributes and/or scroll up to see which attribute should be set.)

The Index object
The Index object stores axis labels for Series and DataFrames.
End of explanation
"""


