I have this data frame where the date columns are 'datetime' type
datetime64[ns]
2014-04-30T00:00:00 000000000
Now I want the date in this format - 2014-04-30 . So I used below code
df['StartingDate2'] = XY['StartingDate'].dt.strftime('%m/%d/%Y')
Now, this works accurately, but converts my date to object type. I read here that In Python, dates are objects. But I want to write my final data frame as a table in redshift and so I need my date columns defined as date for my python data frame. Any suggestion about how to do that , will be greatly appreciated.
Update:
sample dataframe
p1 = {'name': ['johnny', 'tommy', 'bobby', 'rocky', 'jimmy'], 'StartingDate': ['2015-07-14T00:00:00.000000000', '2013-10-30T00:00:00.000000000', '2014-04-30T00:00:00.000000000', '2014-01-27T00:00:00.000000000', '2016-01-15T00:00:00.000000000'], 'Address': ['NY', 'NJ', 'PA', 'NY', 'CA'], 'comment1': ['Very good performance', 'N/A', 'Need to work hard', 'No Comment', 'Not satisfactory'], 'comment2': ['good', 'Meets Expectation', 'N', 'N/A', 'Incompetence']}
XY = pd.DataFrame(data = p1)
XY['today'] = datetime.datetime.now()
When I am using to_datetime() solution - it doesn't work
XY['today2'] = pd.to_datetime(XY['today'], format = '%m/%d/%Y')
XY['StartingDate2'] = pd.to_datetime(XY['today'], format = '%m/%d/%Y')
Alternatively - this works, when strftime() and to_datetime() are used in combination.
XY['StartingDate2'] = XY['StartingDate2'].dt.strftime('%m/%d/%Y')
XY['StartingDate2'] = pd.to_datetime(XY['StartingDate2'])
But although this solution works for the sample data, is not working for me. The data I have looks like this -
array(['2015-09-29T14:34:39.000000000', '2015-10-07T14:13:03.000000000',
'2015-10-07T19:17:50.000000000', ...,
'2017-12-05T14:06:42.000000000', '2017-12-06T16:36:44.000000000',
'2017-12-06T18:26:49.000000000'], dtype='datetime64[ns]'