user9927007
user9927007

Reputation:

Working with Panda Dataframes in Python - Date is being converted to timestamp

I have run into some errors using panda dataframes in python to read and store data in a MySQL/MariaDB database. I am trying to store a calculated date into a dataframe in the form YYYY-MM-DD. I am inputting it into a dataframe but for some reason it is being converted into a timestamp with the format "YYYY-MM-DD HH:MM:SS" for no apparent reason.

I will not show the complete code, but only the parts concerning this date error since I have confirmed the other variables are being passed in properly.

 for j in range(0, final_day):
    DateW = DateP + dt.timedelta(days = j + int(First_day))
    DateWCorr=datetime.date(DateW.year,DateW.month,DateW.day)
    print(DateWCorr,'New')
    if j < length + offset:
        if DateW != ws_date[indexw[j-offset]]:
            doy = (DateW - datetime.date(today.year, 1, 1)).days + 1
            new_record = pd.DataFrame(DateWCorr,columns=['Date'])
            WeatherArray = pd.concat([WeatherArray,new_record])
    else:
        *****OMITTED FOR CLARITY SAKE**********

print("Check",WeatherArray['Date'])
WeatherArray = WeatherArray.sort_values('Date')
WeatherArray = WeatherArray.reset_index()

Showing the syntax of the print statements:

2018-05-06 New
2018-05-07 New
2018-05-08 New
2018-05-09 New
2018-05-10 New
..............

Printing the Date of the WeatherArray.....

2018-04-20 00:00:00
2018-04-21 00:00:00
2018-04-22 00:00:00
2018-04-23 00:00:00
.............(loops through all dates, omitted for clarity)

Why is it converting these to a different format? I cannot for the life of me figure this out.

Upvotes: 0

Views: 69

Answers (1)

Chris
Chris

Reputation: 7278

Is this script pulling from a db table where the field type is datetime?

If so, I'm guessing a datetime coming from the db is being merged with a date, and Python is silently coercing your dates into datetimes.

You could try changing your db schema to be of type date, or explicitly changing the loaded data into dates before putting the new record in your WeatherArray.

Upvotes: 1

Related Questions