Changing date format in python - python

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().

Related

Time data doesn't match format specified error

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')

Issue converting python dataframe datatype from object to datetime

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'

Unable to convert index to date format

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')

How to convert hhmmss.ff format into datetime with pandas

How can I convert the following time format:
hhmmss.ff (like 110241.22 is 11:02:41.22)
into the date/time format with pandas?
I tries to use pandas.to_datetime() but it fails to do the conversion. Here is an example:
hhmmss='110241.22'
pd.to_datetime(hhmmss)
Thanks
You need to specify the format you want to convert the time to. Here's a helpful resource for figuring out what each symbol means. Here's Pandas documentation
pd.to_datetime(df['column_name'], format = '%H%M%S.%f')

In python, how do I convert my string date column to date format using pandas?

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')

Categories

Resources