What is %timeit?
%timeit runs a statement many times and reports:
Average execution time
Best time
Number of loops
It is used inside Jupyter Notebook / IPython.
1. %timeit data['age']
%timeit data['age']
Here:
data is usually a structured NumPy array
'age' is a field (column name)
Accessing it using dictionary-style indexing
This means:
Extract the 'age' field from a structured array.
Example:
data = [Link] ([(25, 170), (30, 180)],
dtype= [('age', 'i4'), ('height', 'i4')])
Then: data ['age'] returns: array ([25, 30])
So %timeit measures how fast NumPy extracts a field using bracket notation.
2. %timeit data_rec['age']
%timeit data_rec['age']
Here:
data_rec is usually a record array (recarray)
Still using bracket access
Record arrays allow two access styles:
data_rec['age']
data_rec.age
This line measures field access time using bracket notation on a record array.
3. %timeit data_rec.age
%timeit data_rec.age
This is attribute-style access.
Instead of: data_rec['age']
You write: data_rec.age
This is only possible if data_rec is a NumPy recarray.
It does the same thing — but internally it works slightly differently.
Why Compare These?
This is usually done to compare:
Method Array Type Access Style Speed
data['age'] Structured array Dictionary-style Fast
data_rec['age'] Recarray Dictionary-style Slightly slower
data_rec.age Recarray Attribute-style Often slowest
Series as a Specialized Dictionary (in Pandas) vs Python Dictionary
In Pandas, a Series can behave like a specialized dictionary because it stores key–value pairs,
where:
Index → Key
Value → Value
Just like a Python dictionary, each element in a Series is accessed using its index label.
Feature Pandas Series Python Dictionary
A one-dimensional labeled array in A built-in Python data structure
Definition
Pandas that stores indexed data. that stores data as key–value pairs.
Structure Index → Value Key → Value
All elements generally belong to same Values can be different data types
Data Type
data type (numeric, string, etc.). in the same dictionary.
Library Built-in in Python (no library
Requires importing Pandas.
Requirement required).
Supports vectorized operations (math Does not support vectorized
Operations
operations on entire data). operations directly.
Indexing Uses index labels to access values. Uses keys to access values.
Mainly used for data analysis and data Used for general data storage and
Data Analysis
manipulation. mapping.
Automatically aligns data based on index
Alignment No automatic alignment feature.
labels during operations.
Example s = [Link]({'a':10,'b':20}) d = {'a':10,'b':20}
Use of Series Object (in Pandas)
A Series object in Pandas is used for:
1. Storing one-dimensional data with index labels.
2. Representing a single column of data in data analysis.
3. Accessing data easily using index labels.
4. Performing mathematical and statistical operations on data.
5. Handling and analyzing structured data in programs written in Python.
DataFrame in Python
A DataFrame in Pandas is a two-dimensional data structure used to store and manipulate data
in rows and columns.
Main Uses:
1. Storing tabular data (like a table or spreadsheet).
2. Handling large datasets efficiently.
3. Performing data analysis and manipulation such as filtering, sorting, and grouping.
4. Reading and writing data from files like CSV, Excel, and databases.
5. Performing statistical operations on data columns in Python.
Pandas Object
In Pandas, Pandas objects are the main data structures used to store, manipulate, and analyze
data in Python.
Main Pandas Objects
1. Series
A one-dimensional labeled array used to store a single column of data.
2. DataFrame
A two-dimensional table-like structure with rows and columns.
3. Panel (older structure)
A three-dimensional data structure used for storing multiple DataFrames.
Uses of Pandas Objects
Store structured data
Perform data analysis
Handle large datasets
Perform statistical and mathematical operations
Read and write data from files (CSV, Excel, etc.)
The Pandas Index Object
The Index object in Pandas is used to label and identify the rows and columns of data
structures such as Series and DataFrame.
It acts like an array of labels that allows users to access and organize data easily in Python.
Key Features of Pandas Index Object
1. Stores axis labels for Series and DataFrame.
2. Immutable (cannot be changed directly) once created.
3. Allows fast data access and alignment.
4. Supports duplicate values if needed.
5. Used to identify and retrieve data efficiently.
DataFrame in Pandas
A DataFrame is the most commonly used data structure in Pandas. It is a two-dimensional
labeled data structure with rows and columns, similar to a table in a database or an Excel
sheet.
Definition
A DataFrame is a table-like structure where:
Rows represent records (observations)
Columns represent variables (features)
Each column can contain different data types
👉 It is built on top of NumPy arrays.
Index as Ordered Set in Pandas
In Pandas, the Index object behaves like an ordered set. This means it stores unique labels in a
fixed order and allows set-like operations while maintaining the order of elements in Python.
Meaning of Ordered Set
An ordered set has two important properties:
1. Order is maintained – The elements remain in the same sequence as they were created.
2. Set operations are supported – Operations such as union, intersection, and difference
can be performed.
#Index as Ordered Set in Pandas
indA = [Link]([1, 3, 5, 7, 9])
indB = [Link]([2, 3, 5, 7, 11])
print([Link](indB))
print([Link](indB))
print(indA.symmetric_difference(indB))
#The Pandas Index Object
ind = [Link]([2, 3, 5, 7, 11])
print(ind)
#Index as Immutable Array
#The Index in many ways operates like an array. For example, we can use standard Python
indexing notation to retrieve values or slices:
print(" first pos data is \n",ind[1])
print("Step 2 data\n",ind[::2])
#Index objects also have many of the attributes familiar from NumPy arrays:
print([Link], [Link], [Link], [Link])
#One difference between Index objects and NumPy arrays is that the indices are immutable—
that is, they cannot be modified via the normal means:
#ind[1] = 0 #TypeError: Index does not support mutable operations