0

I have list of US Holidays:

Holidays = ["January 01, 2019","January 21, 2019","February 14, 2019","February 18, 2019","April 19, 2019","April 21, 2019","May 12, 2019","May 27, 2019","June 16, 2019","July 04, 2019","September 02, 2019","October 14, 2019","October 31, 2019","November 11, 2019","November 28, 2019","December 25, 2019"]

I am converting it to datetinme stamp:

for i in Holidays:
    print(datetime.strptime(i, "%B %d, %Y").date())

I am trying to append it to the another table but i am not getting the expected result

DATE = [datetime.strptime(i, "%B %d, %Y") for i in Holidays]
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
affadam seven
  • 21
  • 1
  • 3

2 Answers2

0

I assume that you're after a string value of the datetime object in your DATE list; based on the example for loop in the question.

Here's how to solve that:

date = [str(datetime.strptime(holiday, "%B %d, %Y").date()) for holiday in holidays]

print(date)

All I've done is change your variable names, add .date() to remove the time and convert it to a string.

Hope that helps

Lucan
  • 2,907
  • 2
  • 16
  • 30
0

Check this.

from datetime import datetime
import pandas as pd

Holidays = ["January 01, 2019","January 21, 2019","February 14, 2019","February 18, 2019","April 19, 2019","April 21, 2019","May 12, 2019","May 27, 2019","June 16, 2019","July 04, 2019","September 02, 2019","October 14, 2019","October 31, 2019","November 11, 2019","November 28, 2019","December 25, 2019"]

converted_holidays = []
for i in Holidays:
  converted_holidays.append(datetime.strptime(i, "%B %d, %Y").date())

data = {'Name': ["Person1","Person2","Person3","Person4","Person5","Person6","Person7","Person8","Person9","Person10","Person11","Person12","Person13","Person14","Person15","Person16"]} 

df = pd.DataFrame(data)

#insert the holidays in dataframe using insert() 
df.insert(0, "Holidays",converted_holidays , True) 

print(df)
Marios Nikolaou
  • 1,326
  • 1
  • 13
  • 24