How to convert pandas Timedelta into datetime? - python

I have a python dataframe.One of its column is like Timedelta('0 days 09:35:00').If I save it into mysql, the time is changed to 34500000000000.
How can I properly save the time into mysql?

First, you need to define an appropriate format to store your timdelta object in mysql. The reason is that the timedelta format type does not exists in mysql as far as I'm aware. For example, if you want to convert the timedelta object in seconds or days as float:
timedelta.total_seconds()
timedelta.days
If you want to convert the timedelta to string:
str(timedelta)

Related

Float to datetime returns wrong output (Same Month) - Python

I have a column of float values which are tweet creation dates. This is the code I used to convert them from float to datetime:
t = 1508054212.0
datetime.utcfromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')
All the values returned belong to October 2017. However, the data is supposed to be collected over multiple months. So the dates should have different months and not just different Hours, Minutes and Seconds.
These are some values which I need to convert:
1508054212.0
1508038548.0
1506890436.0
Request you to suggest an alternative approach to determine the dates. Thank you.
I assumed df['tweet_creation'].loc[1] will return a number like the examples you gave.
Unfortunately, I don't know what f is, but I assumed it was a float.
My answer is inspired by this other answer: Converting unix timestamp string to readable date. You have a UNIX timestamp, so the easiest way is to use it and not convert it as a string.
from datetime import datetime, timedelta
dtobj = datetime.utcfromtimestamp(int(df['tweet_creation'].loc[1])) + timedelta(days=f-int(f))
To have the string representation you can use the function strftime.

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

How to get time value in milliseconds from a date object python?

As part of my new work I need to convert one existing java class to a python one.
person.setDob(String.valueOf(person.getDateOfBirth().getTime()));
Please see the above snippet here how to fetch time in milliseconds from date object in python,
Hope I can use datetime.datetime for this purpose. Please help.
To get a date string with milliseconds (3 decimal places behind seconds), use this:
from datetime import datetime
print datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
OUTPUT 2018-10-04 10:18:32.926
%f is displaying milliseconds
To get milliseconds in python, use the below code.
import datetime
print('Datetime in milliscond using now()',datetime.datetime.now())
print('Datetime in milliscond using utcfromtimestamp()', datetime.datetime.utcfromtimestamp(0))
output looks like below
Datetime in milliscond using now() 2019-03-11 17:34:28.290409
Datetime in milliscond using now() 1970-01-01 00:00:00

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

storing datetime.date type in PyMongo

According to this, it's not possible to save datetime.date instances in MongoDB using Python and pymongo. It says in the FAQ that it's an unsupported type and to use datetime.datetime instead.
However, it's listed (as #9) in the BSON data types page here, so is this just out-of-date, or is there a reason I can't use python this data type?
From mongo docs: http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date
The official BSON specification refers to the BSON Date type as the UTC datetime.
So, there is while it is written as "Date", it's still a datetime in BSON. If you want just date, you can set the hours/minutes/seconds/ms to 0.
Also, as specified on the docs about timestamp:
*NOTE
The BSON timestamp type is for internal MongoDB use. For most cases, in application development, you will want to use the BSON date type. See Date for more information.*
The difference between datetime and timestamp is: datetime is an abstraction of date (day, month, year) and time (hour, min, second), and timestamp is the number of seconds since epoch.

Categories

Resources