I'm a python beginner and am trying to run a time-series analysis with some practice data. I know I have to convert the str date format into datetime. I have already imported pandas and datetime. My dates are originally in the following format:
df.loc[0,'date']
'2017-01-27'
I have tried the line below in order to get the data into a readable format
df['date'] = pd.to_datetime(df['date'], format = '%Y-%m-%d')
Yet I keep getting the error code:
ValueError: time data date doesn't match format specified
Is my format code written correctly or am I missing some syntax anywhere?
Any and all help is much appreciated - Thanks!
try this:
df['date']= pd.to_datetime(df['date'], errors='ignore')
Related
I've been trying to convert a column of a csv file (https://raw.githubusercontent.com/DNR07/CovidPredDatasets/main/covid_19_data.csv) to date time format using pandas.to_datetime() .
I've renamed the Observation date to just Date.
But when I use the aforementioned function I keep getting an error regarding the format of date time.
My exact code is:
c19_url="https://raw.githubusercontent.com/DNR07/CovidPredDatasets/main/covid_19_data.csv"
c19=requests.get(c19_url).content
df=pd.read_csv(io.StringIO(tr.decode('utf-8')))
df.rename(columns={'ObservationDate':'Date','Province/State':'Province_State',
'Country/Region':'Country_Region','Confirmed':'ConfirmedCases',
'Deaths':'Fatalities'},inplace=True)
df['Date'] = pd.to_datetime(df['Date'],format='%m/%d/%Y')
Now if you look at the CSV file, you can see that the format is
%m/%d/%Y, then why is the format in the error changing?
Error:
ValueError: time data '2020-01-22' does not match format '%m/%d/%Y' (match)
Where have I gone wong?
Should try:
df['Date'] = pd.to_datetime(df['Date'],format='%Y-%m-%d')
The issue actually was with the following line of code:
df=pd.read_csv(io.StringIO(tr.decode('utf-8')))
It works if you replace c19 with tr.
I made a silly mistake....
An example of a value in the Date column:
19/08/2017
Previously, I tried:
dividends['Date'] = pd.to_datetime(dividends['Date'])
to convert my column to have a datetime date type. However, when I then created charts out of this dataset (with 'Date' having a datetime date type), it always looked odd:
Having done a bit of trouble shooting online, I think the reason is because of the formatting of my datetime conversion. However, when I try to use this:
dividends['Date'] = pd.to_datetime(dividends['Date'],format='%d-%m-%y')
I get the error message
ValueError: time data '19/08/2017' does not match format '%d-%m-%y' (match)
Why is this, and how do I fix it? Cheers.
Use the following code:(note that I used Y in format):
dividends['Date'] = pd.to_datetime(dividends['Date'],format='%d-%m-%Y')
You could also use the following format:
'%d/%m/%Y'
I have data which is in-64 in the Index with values like "01/11/2018" in the index. It is data that has been imported from a csv. I am unable to convert it to a "01-11-2018" format. How do I do this because I get an error message:
'time data 0 does not match format '%Y' (match)'
I got the data from the following website:
https://www.nasdaq.com/symbol/spy/historical
and you can find a ' Download this file in Excel Format ' icon at the bottom.
import datetime
spyderdat.index = pd.to_datetime(spyderdat.index, format='%Y')
spyderdat.head()
How do I format this correctly?
Thanks a lot.
Your format string must match exactly:
import datetime
spyderdat.index = pd.to_datetime(spyderdat.index, format='%d/%m/%Y')
spyderdat.head()
Example w/o spyder:
import datetime
date = "1/11/2018"
print(datetime.datetime.strptime(date,"%d/%m/%Y"))
Output:
2018-11-01 00:00:00
You can strftime this datetime then anyhow you like. See link for formats. Or you store datetimes.
Assuming your input is a string, simply converting the / to - won't fix the issue.
The real problem is that you've told to_datetime to expect the input string to be only a 4-digit year but you've handed it an entire date, days and months included.
If you meant to use only the year portion you should manually extract the year first with something like split.
If you meant to use the full date as a value, you'll need to change your format to something like %d/%m/%Y. (Although I can't tell if your input is days first or months first due to their values.)
The easy way is to try this
datetime.datetime.strptime("01/11/2018", '%d/%m/%Y').strftime('%d-%m-%Y')
I am unable to convert my date column from my data frame from a string to date format. I've tried using the lambda date conversion code in the image above, as well as a few other methods and I can't seem to make it work. It appears to possibly be because my 'variable' column does not appear to be like the rest of the columns (I think that it is maybe "indexed?"). Help would be much appreciate!
enter image description here
Use to_datetime:
Hotsprings2['variable'] = pd.to_datetime(Hotsprings2['variable'], format='%Y-%m-%d')
I have a pandas dataframe with a column containing a date; the format of the original string is YYYY/DD/MM HH:MM:SS.
I am trying to convert the string into a datetime format, by using
df['Date']=pd.to_datetime(df['Data'], errors='coerce')
but plotting it I can see it doesn't recognize the correct format.
Can you help me to understand whether there is an option to give python the correct format to read the column?
I have seen the format tag for to_datetime function, but I can't use it correctly.
Thanks a lot for your help!
Try this:
df['Date'] = pd.to_datetime(df['Data'], format='%Y/%d/%m %H:%M:%S')
It looks like you're using a non-standard date format. It should be YYYY-MM-DD. Try formating with the strptime() method.
time.strptime('2016/15/07', '%Y/%d/%m')
If you need to get it to a string after that use time.strftime().