
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to print the first and last three days from a given time series data
Assume, you have time series and the result for the first and last three days from the given series as,
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
To solve this, we will follow the steps given below −
Solution
Define a series and store it as data.
Apply pd.date_range() function inside start date as ‘2020-01-01’ and periods = 5, freq =’2D’ and save it as time_series
time_series = pd.date_range('2020-01-01', periods = 5, freq ='2D')
Set date.index = time_series
Print the first three days using data.first(’3D’) and save it as first_day
first_day = data.first('3D')
Print the last three days using data.last(’3D’) and save it as last_day
last_day = data.last('3D')
Example
Let’s check the following code to get a better understanding −
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 print("time series:\n",data) first_day = data.first('3D') print("first three days:\n",first_day) last_day = data.last('3D') print("last three days:\n",last_day)
Output
time series: 2020-01-01 Chennai 2020-01-03 Delhi 2020-01-05 Mumbai 2020-01-07 Pune 2020-01-09 Kolkata Freq: 2D, dtype: object 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
- Related Articles
- Write a program in Python to resample a given time series data and find the maximum month-end frequency
- Write a Python program to find the maximum value from first four rows in a given series
- Write a program in Python to read CSV data from a file and print the total sum of last two rows
- Write a program in Python to print the power of all the elements in a given series
- Write a program in Python to print the day of the year in a given date series
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- Write a program in Python to slice substrings from each element in a given series
- Python program to get first and last elements from a tuple
- Write a program in Python to print numeric index array with sorted distinct values in a given series
- Write a Golang program to print the Fibonacci series
- Write a program in Python to print the most frequently repeated element in a series
- Write a Python program to shuffle all the elements in a given series
- Write a program in Python to print the elements in a series between a specific range
- Write a program in Python to count the total number of integer, float and object data types in a given series
- How to print the days in a given month using Python?

Advertisements