I have pandas column row['date'] which contains date in format 11/05/2015. I am trying to insert it into mysql db but having problems due to incorrect format of date field data. It has to be converted into 2015-11-05 in order to be inserted. Without storing the new value in variable how can I convert the date into required format?
Current format: 11/05/2015
Required format: 2015-11-05
Is the current format mm/dd/yyyy? If so
from datetime import datetime
row['date'] = datetime.strptime(row['date'], '%m/%d/%Y').strftime('%Y-%m-%d')
Use dateutil.parser,
This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time.
Here is a MWE.
from dateutil.parser import parse
current_date = '11/05/2015'
required_date = parse(current_date).strftime('%Y-%m-%d')
PS: to explicitly distinguish between DM and MD, pass the argument dayfirst=True/False to parse, i.e. dayfirst=True represents DM and dayfirst=False represents MD.
This should do the job, w/o needing datetime:
"{2}-{0}-{1}".format(*(original_date.split("/")))
Related
This is my data :
dates = np.arange("2018-01-01", "2021-12-31", dtype="datetime64[D]")
I now want to convert from :
"2018-01-01" -> "Jan-01-2018" ["Monthname-day-year"] format
How to i do this ?
Is it possible to initialize this in the way we want to convert ?
Can i use something like:
for i in dates:
i = i.replace(i.month,i.strftime("%b"))
You can try this:
from datetime import datetime
dates = np.arange("2018-01-01", "2021-12-31", dtype="datetime64[D]")
result_dates = []
for date in dates.astype(datetime):
result_dates.append(date.strftime("%b-%d-%Y"))
But you will need to convert result dates as shown in the code
I feel compelled to elaborate on Silvio Mayolo's very relevant but ostensibly ignored comment above. Python stores a timestamp as structure (see How does Python store datetime internally? for more information) Hence, the DateTime does not as such have a 'format'. A format only becomes necessary when you want to print the date because you must first convert the timestamp to a string. Thus, you do NOT need to initialise any format. You only need to declare a format when the time comes to print the timestamp.
While you CAN store the date as a string in your dataframe index in a specific format, you CANNOT perform time related functions on it without first converting the string back to a time variable. ie current_time.hour will return an integer with the current hour if current_time is a datetime variable but will crash if it is a string formatted as a timestamp (such as "2023-01-15 17:23").
This is important to understand, because eventually you will need to manipulate the variables and need to understand whether you are working with a time or a string.
I get a data export, and for the date field they all come in as a string like: "1 day ending 01-11-2021".
I want to be able to load these into a database without a lot of manual work changing the dates.
Any advice?
Assuming that you want to capture the part of string with the date:
from datetime import datetime
import re
date_string = "1 day ending 01-11-2021"
#Use regex to find a date-like pattern
match = re.search(r'\d{2}-\d{2}-\d{4}', date_string)
#Now save the matched string as a datetime object
date = datetime.strptime(match.group(), '%d-%m-%Y').date()
This operation can of course be applied to something like a Pandas DataFrame column-wise. Python uses strftime routines for generating DateTime objects.
Documentation for strptime
I get time data from API response like '2020-02-25T20:53:06.706401+07:00'. Now I want to convert to %Y-%m-%d %H:%M:%s format. But I do not know exactly standard format of that time data.
Help me find the time format!
In your case you can use datetime.fromisoformat:
from datetime import datetime
datetime_object = datetime.fromisoformat("2020-02-25T20:53:06.706401+07:00")
print(datetime_object.strftime("%Y-%m-%d %H:%M:%s"))
Prints
2020-02-25 20:53:1582656786
Other options:
Use the third party dateutil library
Use datetime.strptime which parses the string according to format
You can convert to a datetime object and then optionally recreate the string in a new format as follows:
from datetime import datetime
d = "2020-02-25T20:53:06.706401+07:00"
dt = datetime.strptime(d, "%Y-%m-%dT%H:%M:%S.%f%z")
# Note the capital S
new = dt.strftime("%Y-%m-%d %H:%M:%S")
However the new value here has lost the timezone offset information. I assume that's OK for you. I also used %S instead of %s since I assume that's really what you want. The lowercase %s wouldn't really make sense, and is also not truly supported by Python.
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 do I check if a variable contains a datetime and the format it to just include the date, hour, minutes and seconds. Not milliseconds.
The issue I'm having is that the variable isn't in datetime format as it can either contain text or a date time.
return isinstance(new_value, datetime.datetime)
Therefore running the command above returns false.
If new_value contains 2016-05-24 09:26:51.754000+00:00 how do I format it to look like this : 2016-05-24 09:26:51
At first you need to convert date string to the datetime object, in this way you can check it with 'isinstance()', but I want to suggest better solution, as I think :), because at the Python 2.7, so still popular, we have issue with timezone offset ( %z ) when convert date string to datetime object, so here is my solution without 'strptime' :
from dateutil import parser
date = '2016-05-24 09:26:51.754000+00:00'
try:
# something if format correct
parsed_date = parser.parse(date)
# formatting
print parsed_date.strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
# something if format incorrect
print 'Wrong date format'
Sure, please don't forget to write :
$pip install python-dateutil