Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting data object
#1
I am pulling a csv from a database. With that the date is an object, I am attempting to convert it to be in datetime64[ns].

My code is as follows:

#!/usr/bin/env python
__author__ = "Michael Brown"
__license__ = "Based off of sript by Sreenivas Bhattiprolu of Python for Microscopists"

import pandas as pd
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib

CVD = pd.read_csv('https://opendata.arcgis.com/datasets/18582de727934249b92c52542395a3bf_0.csv')
#print(CVD.head())
print(CVD.dtypes)
CVD['DATE'] = [dt.datetime.strptime(x,'%Y/%m/%d %H:%M:%S') 
               for x in CVD['DATE']] 
print(CVD.dtypes)
the output is the following

ValueError: time data '2020/03/04 15:00:00+00' does not match format '%Y/%m/%d %H:%M:%S%z'
I am confused what I am missing. I believe it has something to do with the +00 format for the timezone offset.

Any assistance would be great!
Reply
#2
Your code doesn't seem to match your error.

But you're right. A timezone offset would be a + or - followed by 4 digits. You only have 2. So you can either manually strip the timezone and not parse it, or you could add a couple of zeros as the minute portion and parse it with %z
Reply
#3
sorry about that. I thought I did an updated copy. Here is the updated code.

#!/usr/bin/env python
__author__ = "Michael Brown"
__license__ = "Based off of sript by Sreenivas Bhattiprolu of Python for Microscopists"
 
import pandas as pd
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib
 
CVD = pd.read_csv('https://opendata.arcgis.com/datasets/18582de727934249b92c52542395a3bf_0.csv')
#print(CVD.head())
print(CVD.dtypes)
CVD['DATE'] = [dt.datetime.strptime(x,'%Y/%m/%d %H:%M:%S%z') 
               for x in CVD['DATE']] 
print(CVD.dtypes)
Reply
#4
Thank you for your information. I added two additional "0"s. This was able to resolve the issue. What is your opinion on this solution?

#!/usr/bin/env python
__author__ = "Michael Brown"
__license__ = "Based off of sript by Sreenivas Bhattiprolu of Python for Microscopists"

import pandas as pd
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib

CVD = pd.read_csv('https://opendata.arcgis.com/datasets/18582de727934249b92c52542395a3bf_0.csv')
#print(CVD.head())
CVD['ndate'] = CVD['DATE']+'00'

print(CVD.dtypes)
CVD['ndate'] = [dt.datetime.strptime(x,'%Y/%m/%d %H:%M:%S%z') 
               for x in CVD['ndate']] 
print(CVD.dtypes)
Reply
#5
If you're sure the format is always the same (like it's generated by a program and that's always used), then it seems workable to me.
mbrown009 likes this post
Reply
#6
(May-24-2021, 03:56 AM)bowlofred Wrote: If you're sure the format is always the same (like it's generated by a program and that's always used), then it seems workable to me.

Yes it is the same way all the time. I am not sure why they do not do the full format.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  bytearray object - why converting to ascii? Chepilo 2 1,530 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Strategy on updating edits back to data table and object variables hammer 0 1,163 Dec-11-2021, 02:58 PM
Last Post: hammer
  Converting data in CSV and TXT to dictionary kam_uk 3 1,950 Dec-22-2020, 08:43 PM
Last Post: bowlofred
  converting string object inside a list into an intiger bwdu 4 2,552 Mar-31-2020, 10:36 AM
Last Post: buran
  'tuple' object has no attribute 'data' gregpederseng 2 3,280 Feb-25-2020, 08:01 PM
Last Post: gregpederseng
  Converting query string as a condition for filter data. shah_entrance 1 1,750 Jan-14-2020, 09:22 AM
Last Post: perfringo
  converting data sizes like: 4k, 32k, 4m, 16m, 1g, etc Skaperen 1 2,147 Jul-16-2019, 01:37 AM
Last Post: Skaperen
  Converting raw data into panel data jason413 1 2,419 May-25-2018, 06:10 PM
Last Post: Larz60+
  List data-member of object not updating inside loop. Sagar 2 3,362 Aug-30-2017, 01:07 PM
Last Post: Sagar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020