

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 Golang program to print the Fibonacci series
- Write a Python program to shuffle all the elements in a given series
- Write a program in Python to slice substrings from each element in a given series
- How to print the days in a given month using Python?
- Write a program in Python to round all the elements in a given series
- Write a program in Python to print the most frequently repeated element in a series
- Write a program in Python to print the elements in a series between a specific range
- Write a program to truncate a dataframe time series data based on index value
- C# Program to get the last write time of a file
Advertisements