Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a program in Python to print the first and last three days from a given time series data
When working with time series data in Pandas, you often need to extract specific time periods. The first() and last() methods allow you to retrieve data from the beginning and end of a time series based on a time offset.
Creating Time Series Data
First, let's create a time series with city names indexed by dates ?
import pandas as pd
# Create a series with city names
data = pd.Series(['Chennai', 'Delhi', 'Mumbai', 'Pune', 'Kolkata'])
# Create a date range with 2-day frequency
time_series = pd.date_range('2020-01-01', periods=5, freq='2D')
# Set the date range as index
data.index = time_series
print("Time series data:")
print(data)
Time series data: 2020-01-01 Chennai 2020-01-03 Delhi 2020-01-05 Mumbai 2020-01-07 Pune 2020-01-09 Kolkata Freq: 2D, dtype: object
Extracting First and Last Three Days
Use the first() and last() methods with a time offset to get data from specific periods ?
import pandas as pd
data = pd.Series(['Chennai', 'Delhi', 'Mumbai', 'Pune', 'Kolkata'])
time_series = pd.date_range('2020-01-01', periods=5, freq='2D')
data.index = time_series
# Get first three days of data
first_three_days = data.first('3D')
print("First three days:")
print(first_three_days)
print("\n" + "="*30 + "\n")
# Get last three days of data
last_three_days = data.last('3D')
print("Last three days:")
print(last_three_days)
First three days: 2020-01-01 Chennai 2020-01-03 Delhi Freq: 2D, dtype: object ============================== Last three days: 2020-01-07 Pune 2020-01-09 Kolkata Freq: 2D, dtype: object
How It Works
The first() and last() methods work with time offsets:
- first('3D'): Returns data from the start date up to 3 days later
- last('3D'): Returns data from 3 days before the end date to the end
- Time offset '3D': Represents a 3-day period
Alternative Approaches
You can also use head() and tail() to get a specific number of records ?
import pandas as pd
data = pd.Series(['Chennai', 'Delhi', 'Mumbai', 'Pune', 'Kolkata'])
time_series = pd.date_range('2020-01-01', periods=5, freq='2D')
data.index = time_series
# Get first 2 records
print("First 2 records using head():")
print(data.head(2))
print("\n" + "="*30 + "\n")
# Get last 2 records
print("Last 2 records using tail():")
print(data.tail(2))
First 2 records using head(): 2020-01-01 Chennai 2020-01-03 Delhi Freq: 2D, dtype: object ============================== Last 2 records using tail(): 2020-01-07 Pune 2020-01-09 Kolkata Freq: 2D, dtype: object
Conclusion
Use first() and last() methods with time offsets like '3D' to extract time-based subsets from time series data. For record-based extraction, use head() and tail() methods instead.
Advertisements
