Tag Archives: python

Pandas Example – Write a Pandas program to convert integer or float epoch times to Timestamp and DatetimeIndex

(Python Example for Beginners)   Write a Pandas program to convert integer or float epoch times to Timestamp and DatetimeIndex.   Sample Solution: Python Code : import pandas as pd dates1 = pd.to_datetime([1329806505, 129806505, 1249892905, 1249979305, 1250065705], unit=’s’) print(“Convert integer or float epoch times to Timestamp and DatetimeIndex upto second:”) print(dates1) print(“nConvert integer or float …

Pandas Example – Write a Pandas program to extract the day name from a specified date

(Python Example for Beginners)   Write a Pandas program to extract the day name from a specified date. Add 2 days and 1 business day with the specified date.   Sample Solution: Python Code : import pandas as pd newday = pd.Timestamp(‘2020-02-07’) print(“First date:”) print(newday) print(“nThe day name of the said date:”) print(newday.day_name()) print(“nAdd 2 …

Pandas Example – Write a Pandas program to generate sequences of fixed-frequency dates and time spans intervals

(Python Example for Beginners)   Write a Pandas program to generate sequences of fixed-frequency dates and time spans intervals.   Sample Solution: Python Code : import pandas as pd print(“Sequences of fixed-frequency dates and time spans (1 H):n”) r1 = pd.date_range(‘2030-01-01′, periods=10, freq=’H’) print(r1) print(“nSequences of fixed-frequency dates and time spans (3 H):n”) r2 = …

Pandas Example – Write a Pandas program to find the all the business quarterly begin and end dates of a specified year

(Python Example for Beginners)   Write a Pandas program to find the all the business quarterly begin and end dates of a specified year.   Sample Solution: Python Code : import pandas as pd q_start_dates = pd.date_range(‘2020-01-01’, ‘2020-12-31′, freq=’BQS-JUN’) q_end_dates = pd.date_range(‘2020-01-01’, ‘2020-12-31′, freq=’BQ-JUN’) print(“All the business quarterly begin dates of 2020:”) print(q_start_dates.values) print(“nAll the …

Pandas Example – Write a Pandas program to calculate all Thursdays between two given days

(Python Example for Beginners)   Write a Pandas program to calculate all Thursdays between two given days.   Sample Solution: Python Code : import pandas as pd thursdays = pd.date_range(‘2020-01-01’, ‘2020-12-31’, freq=”W-THU”) print(“All Thursdays between 2020-01-01 and 2020-12-31:n”) print(thursdays.values) Sample Output: All Thursdays between 2020-01-01 and 2020-12-31: [‘2020-01-02T00:00:00.000000000’ ‘2020-01-09T00:00:00.000000000’ ‘2020-01-16T00:00:00.000000000’ ‘2020-01-23T00:00:00.000000000’ ‘2020-01-30T00:00:00.000000000’ ‘2020-02-06T00:00:00.000000000’ ‘2020-02-13T00:00:00.000000000’ ‘2020-02-20T00:00:00.000000000’ …

Pandas Example – Write a Pandas program to subtract two timestamps of same time zone or different time zone

(Python Example for Beginners)   Write a Pandas program to subtract two timestamps of same time zone or different time zone.   Sample Solution: Python Code : import pandas as pd print(“Subtract two timestamps of same time zone:”) date1 = pd.Timestamp(‘2019-03-01 12:00′, tz=’US/Eastern’) date2 = pd.Timestamp(‘2019-04-01 07:00′, tz=’US/Eastern’) print(“Difference: “, (date2-date1)) print(“nSubtract two timestamps of …

Pandas Example – Write a Pandas program to remove the time zone information from a Time series data

(Python Example for Beginners)   Write a Pandas program to remove the time zone information from a Time series data.   Sample Solution: Python Code : import pandas as pd date1 = pd.Timestamp(‘2019-01-01′, tz=’Europe/Berlin’) date2 = pd.Timestamp(‘2019-01-01′, tz=’US/Pacific’) date3 = pd.Timestamp(‘2019-01-01′, tz=’US/Eastern’) print(“Time series data with time zone:”) print(date1) print(date2) print(date3) print(“nTime series data without …

Pandas Example – Write a Pandas program to create a time series object with a time zone

(Python Example for Beginners)   Write a Pandas program to create a time series object with a time zone.   Sample Solution: Python Code : import pandas as pd print(“Timezone: Europe/Berlin:”) print(“Using pytz:”) date_pytz = pd.Timestamp(‘2019-01-01’, tz = ‘Europe/Berlin’) print(date_pytz.tz) print(“Using dateutil:”) date_util = pd.Timestamp(‘2019-01-01’, tz = ‘dateutil/Europe/Berlin’) print(date_util.tz) print(“nUS/Pacific:”) print(“Using pytz:”) date_pytz = pd.Timestamp(‘2019-01-01’, …

Pandas Example – Write a Pandas program to check if a day is a business day (weekday) or not

(Python Example for Beginners)   Write a Pandas program to check if a day is a business day (weekday) or not.   Sample Solution: Python Code : import pandas as pd def is_business_day(date): return bool(len(pd.bdate_range(date, date))) print(“Check busines day or not?”) print(‘2020-12-01: ‘,is_business_day(‘2020-12-01’)) print(‘2020-12-06: ‘,is_business_day(‘2020-12-06’)) print(‘2020-12-07: ‘,is_business_day(‘2020-12-07’)) print(‘2020-12-08: ‘,is_business_day(‘2020-12-08’)) Sample Output: Check busines day or …

Pandas Example – Write a Pandas program to create a time series object that has time indexed data

(Python Example for Beginners)   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.   Sample Solution: Python Code : import pandas as pd index = pd.DatetimeIndex([‘2011-09-02’, ‘2012-08-04’, ‘2015-09-03’, ‘2010-08-04’, ‘2015-03-03’, ‘2011-08-04’, ‘2015-04-03’, ‘2012-08-04’]) s_dates …