Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas Time Series: Exercises, Practice, Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

Pandas Time Series [ 32 exercises with solution]

1. Write a Pandas program to create Go to the editor
a) Datetime object for Jan 15 2012.
b) Specific date and time of 9:20 pm.
c) Local date and time.
d) A date without time.
e) Current date.
f) Time from a datetime.
g) Current local time.
Click me to see the sample solution

2. Write a Pandas program to create Go to the editor
a) a specific date using timestamp.
b) date and time using timestamp.
c) a time adds in the current local date using timestamp.
d) current date and time using timestamp.
Click me to see the sample solution

3. Write a Pandas program to create a date from a given year, month, day and another date from a given string formats. Go to the editor
Click me to see the sample solution

4. Write a Pandas program to print the day after and before a specified date. Also print the days between two given dates. Go to the editor
Click me to see the sample solution

5. Write a Pandas program to create a time-series with two index labels and random values. Also print the type of the index. Go to the editor
Click me to see the sample solution

6. Write a Pandas program to create a time-series from a given list of dates as strings. Go to the editor
Click me to see the sample solution

7. Write a Pandas program to create a time series object that has time indexed data. Also select the dates of same year and select the dates between certain dates. Go to the editor
Click me to see the sample solution

8. Write a Pandas program to create a date range using a startpoint date and a number of periods. Go to the editor
Click me to see the sample solution

9. Write a Pandas program to create a whole month of dates in daily frequencies. Also find the maximum, minimum timestamp and indexs. Go to the editor
Click me to see the sample solution

10. Write a Pandas program to create a time series using three months frequency. Go to the editor
Click me to see the sample solution

11. Write a Pandas program to create a sequence of durations increasing by an hour. Go to the editor
Click me to see the sample solution

12. Write a Pandas program to convert year and day of year into a single datetime column of a dataframe.Go to the editor
Click me to see the sample solution

13. Write a Pandas program to create a series of Timestamps from a DataFrame of integer or string columns. Also create a series of Timestamps using specified columns. Go to the editor
Click me to see the sample solution

14. Write a Pandas program to check if a day is a business day (weekday) or not. Go to the editor
Click me to see the sample solution

15. Write a Pandas program to get a time series with the last working days of each month of a specific year. Go to the editor
Click me to see the sample solution

16. Write a Pandas program to create a time series combining hour and minute. Go to the editor
Click me to see the sample solution

17. Write a Pandas program to convert unix/epoch time to a regular time stamp in UTC. Also convert the said timestamp in to a given time zone. Go to the editor
Click me to see the sample solution

18. Write a Pandas program to create a time series object with a time zone. Go to the editor
Click me to see the sample solution

19. Write a Pandas program to remove the time zone information from a Time series data. Go to the editor
Click me to see the sample solution

20. Write a Pandas program to subtract two timestamps of same time zone or different time zone. Go to the editor
Click me to see the sample solution

21. Write a Pandas program to calculate all Thursdays between two given days. Go to the editor
Click me to see the sample solution

22. Write a Pandas program to find the all the business quarterly begin and end dates of a specified year. Go to the editor
Click me to see the sample solution

23. Write a Pandas program to generate sequences of fixed-frequency dates and time spans intervals. Go to the editor
Click me to see the sample solution

24. Write a Pandas program to generate time series combining day and intraday offsets intervals. Go to the editor
Click me to see the sample solution

25. Write a Pandas program to extract the day name from a specified date. Add 2 days and 1 business day with the specified date. Go to the editor
Click me to see the sample solution

26. Write a Pandas program to convert integer or float epoch times to Timestamp and DatetimeIndex. Go to the editor
Click me to see the sample solution

27. Write a Pandas program to calculate one, two, three business day(s) from a specified date. Also find the next business month end from a specific date. Go to the editor
Click me to see the sample solution

28. Write a Pandas program to create a period index represent all monthly boundaries of a given year. Also print start and end time for each period object in the said index. Go to the editor
Click me to see the sample solution

29. Write a Pandas program create a series with a PeriodIndex which represents all the calendar month periods in 2029 and 2031. Also print the values for all periods in 2030. Go to the editor
Note: PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time such as particular years, quarters, months, etc.
Click me to see the sample solution

30. Write a Pandas program to generate holidays between two dates using the US federal holiday calendar. Go to the editor
Click me to see the sample solution

31. Write a Pandas program to create a monthly time period and display the list of names in the current local scope. Go to the editor
Click me to see the sample solution

32. Write a Pandas program to create a yearly time period from a specified year and display the properties of this period. Go to the editor
Click me to see the sample solution

Python Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m