How to change text string to date to upload into database - python

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

Related

How to convert the format all the values in a date-time array?

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.

Python datetime without Y-M-D

I'm converting an string type of Time series to datetime in Python and I'm so confused that why is my datetime always display the result I don't expect. \n
what I want is shown in my img here
import datetime
time = '23:30:00' # Time in string format
dt=datetime.datetime.strptime(time, '%H:%M:%S')
print(dt.time()) # time method will only return the time
I hope this helps
You should put your question in the question, not some off-site illustration. We do have code blocks available. Also, you converted to Pandas datetime, not Python datetime. Both of these have "date" in their name because they do contain the date. You could represent just a time using e.g. Pandas timedelta or Python datetime.time. The format you pass to panads.to_datetime is how to parse the input, not how to display the result.
You have converted your string Series to a Series of pd.Timestamp. Internally a Timestamp is a number of nanoseconds from 1970-01-01 00:00:00.
The correct way to format a date in pandas is to convert it to a string with .dt.strfime, *when you no longer need to process it as a Timestamp.
TL/DR:
if you want it in HH:MM:SS format leave it in string dtype
if you need to process it as a Timestampand yet have it in HH:MM:SS format, convert it to Timestamp, process it and when done convert it back to a string

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

Convert date format to insert into mysql db

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("/")))

Python: trouble converting string into date

I am somewhat new to Python and have a seemingly simple question.
I have a python script that interacts with an API (RHN Satellite if you're curious). This API returns a date in the form of a string and it always trims leading 0's. For example, 6/1/13 or 10/9/12. I need to convert this string to a date and determine the day of the year it is.
Here is what I know:
today = datetime.datetime.now()
print today.strftime('%j')
...will return today's day of year (175). This works fine for a datetime object but I am having trouble converting the string given by the API to an actual date. If I use:
date = datetime.datetime.strptime(var, '%m/%d/$y')
I get error:
ValueError: time data '5/2/13' does not match format '%m/%d/$y'
I'm guessing because it's expecting leading 0's ? How do I get around this?
In the end, I am trying to subtract the variable date given from the current date but I can't do that until I convert the string.
Thanks for the help!
I think you just have a typo, use %y instead of $y:
date = datetime.datetime.strptime(var, '%m/%d/%y')
This code works for me, provided you change $y to %y in the format code.
Correct the $y to %y and I'd use format instead of strftime:
from datetime import datetime
print format(datetime.strptime('5/2/13', '%m/%d/%y'), '%j')

Categories

Resources