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')
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'm getting Java date format strings (yyyymmdd) as input. I need to convert them to Python-based format (%Y%m%d) or just use these to get the current date in that format in Python. e.g. I want to achieve the following in Python:
print(current_time.strftime('yyyymmdd')
Result:
20210426
Convert python date format (%Y) to java (yyyy)
Similar question but its the other way around and I can't use Template like this since there is no delimiter
I'm not sure I understand your question. Here's how to achieve the same output with python3 using datetime module:
import datetime
today_date_obj = datetime.date.today()
formatted_date_string = str(today_date_obj.strftime('%Y%m%d'))
print(formatted_date_string)
Dates in python are objects. To convert your date to string, use str().
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'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
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().