How to convert millisecond time stamp to normal date in Python? - python

I have a data frame with a column with values in millisecond time stamp.(df['Millisecond'])
What I want to do is to convert all values of that column into normal dates. Ex. 2017-04-27 04:55:00

You need python's datetime package to do that:
import datetime
date = datetime.datetime.fromtimestamp(milliseconds/1000.0)
date = date.strftime('%Y-%m-%d %H:%M:%S')

you can do this by using to_datetime function https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html.
df['Millisecond'] = pd.to_datetime(df['Millisecond'], unit='ms')

Related

converting arbitrary date time format to panda timeseries

I'm trying to convert a column in a dataframe to timeseries, the values in the column are strings and they are in the following form:
12/10/202110:42:05.397
which means 12-10-2021 at 10:42:05 and 397 milliseconds. This is the format that Labview is saving the data into a file.
I'm trying to use the following command, but I can't figure out how to define the format for my case:
pd.to_datetime(df.DateTime, format=???)
Note that there is no space between year 2021 and hour 10
Use:
df['dt'] = pd.to_datetime(df['DateTime'], format='%d/%m/%Y%H:%M:%S.%f')
print(df)
# Output
DateTime dt
0 12/10/202110:42:05.397 2021-10-12 10:42:05.397
Setup:
df = pd.DataFrame({'DateTime': ['12/10/202110:42:05.397']})
As suggested by #RaymondKwok, use the documentation:
strftime() and strptime() Format Codes

Convert date time to day and time

I have a variable in a df that looks like this
Datetime
10/27/2020 2:28:28 PM
8/2/2020 3:30:18 AM
6/15/2020 5:38:19 PM
How can I change it to this using python?
Date Time
10/27/2020 14:28:28
8/2/2020 3:30:18
6/15/2020 17:38:19
I understand how to separate date and time, but unsure of how to convert it to 24 hour time.
I think this is source you want:
from dateutil.parser import parse
dt = parse("10/27/2020 2:28:28")
print(dt)
# 2020-10-27 02:28:28
# Create Date
date=f"{str(dt.year)}/{str(dt.month)}/{str(dt.day)}"
# Create Time
time=f"{str(dt.hour)}:{str(dt.minute)}:{str(dt.second)}"
You can use pd.to_datetime to convert a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object. Then, you can use the accessor object for datetimelike properties of the Series values (Series.dt()) to obtain the time, that will be already in the desired format.
You can also use dt.strftime to format the output string which supports the same string format as the python standard library.
df['Datetime'] = pd.to_datetime(df.Datetime)
df['Date'] = df.Datetime.dt.strftime('%m/%d/%Y')
df['Time'] = df.Datetime.dt.time
print(df)
Datetime Date Time
0 2020-10-27 14:28:28 10/27/2020 14:28:28
1 2020-08-02 03:30:18 08/02/2020 03:30:18
2 2020-06-15 17:38:19 06/15/2020 17:38:19

How to convert milliseconds to date and time format?

I have a value in milliseconds in a Python program. For example: 1557975599999
And I would like to convert it to a string with days and hours, minutes, seconds. How can I do this?
To convert unix timestamp to datetime, you can use datetime.fromtimestamp(). The only problem, that your timestamp is in miliseconds, but function expect timestamp in seconds. To cut miliseconds you can divide timestamp to 1000.
Code:
from datetime import datetime
a = 1557975599999
date = datetime.fromtimestamp(a // 1000)
print(date)
Output:
2019-05-16 05:59:59
Upd.
#Daniel in comments noticed that fromtimestamp() accept floats, so we can save miliseconds from original timestamp. All we need is just to remove one symbol :D
date = datetime.fromtimestamp(a / 1000)
With Pandas’ to_datetime()
import pandas as pd
pd.to_datetime(a, unit='ms')
# Or with a dataframe(column):
df['date'] = pd.to_datetime(df['Millisecond_time'], unit='ms')

Comparison between datetime and datetime64[ns] in pandas

I'm writing a program that checks an excel file and if today's date is in the excel file's date column, I parse it
I'm using:
cur_date = datetime.today()
for today's date. I'm checking if today is in the column with:
bool_val = cur_date in df['date'] #evaluates to false
I do know for a fact that today's date is in the file in question. The dtype of the series is datetime64[ns]
Also, I am only checking the date itself and not the timestamp afterwards, if that matters. I'm doing this to make the timestamp 00:00:00:
cur_date = datetime.strptime(cur_date.strftime('%Y_%m_%d'), '%Y_%m_%d')
And the type of that object after printing is datetime as well
For anyone who also stumbled across this when comparing a dataframe date to a variable date, and this did not exactly answer your question; you can use the code below.
Instead of:
self.df["date"] = pd.to_datetime(self.df["date"])
You can import datetime and then add .dt.date to the end like:
self.df["date"] = pd.to_datetime(self.df["date"]).dt.date
You can use
pd.Timestamp('today')
or
pd.to_datetime('today')
But both of those give the date and time for 'now'.
Try this instead:
pd.Timestamp('today').floor('D')
or
pd.to_datetime('today').floor('D')
You could have also passed the datetime object to pandas.to_datetime but I like the other option mroe.
pd.to_datetime(datetime.datetime.today()).floor('D')
Pandas also has a Timedelta object
pd.Timestamp('now').floor('D') + pd.Timedelta(-3, unit='D')
Or you can use the offsets module
pd.Timestamp('now').floor('D') + pd.offsets.Day(-3)
To check for membership, try one of these
cur_date in df['date'].tolist()
Or
df['date'].eq(cur_date).any()
When converting datetime64 type using pd.Timestamp() it is important to note that you should compare it to another timestamp type. (not a datetime.date type)
Convert a date to numpy.datetime64
date = '2022-11-20 00:00:00'
date64 = np.datetime64(date)
Seven days ago - timestamp type
sevenDaysAgoTs = (pd.to_datetime('today')-timedelta(days=7))
convert date64 to Timestamp and see if it was in the last 7 days
print(pd.Timestamp(pd.to_datetime(date64)) >= sevenDaysAgoTs)

Extract Date from excel and append it in a list using python

I have an column in excel which has dates in the format ''17-12-2015 19:35". How can I extract the first 2 digits as integers and append it to a list? In this case I need to extract 17 and append it to a list. Can it be done using pandas also?
Code thus far:
import pandas as pd
Location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(Location)
time = df['Creation Date'].tolist()
print (time)
You could extract the day of each timestamp like
from datetime import datetime
import pandas as pd
location = r'F:\Analytics Materials\files\paymenttransactions.csv'
df = pd.read_csv(location)
timestamps = df['Creation Date'].tolist()
dates = [datetime.strptime(timestamp, '%d-%m-%Y %H:%M') for timestamp in timestamps]
days = [date.strftime('%d') for date in dates]
print(days)
The '%d-%m-%Y %H:%M'and '%d' bits are format specififers, that describe how your timestamp is formatted. See e.g. here for a complete list of directives.
datetime.strptime parses a string into a datetimeobject using such a specifier. dateswill thus hold a list of datetime instances instead of strings.
datetime.strftime does the opposite: It turns a datetime object into string, again using a format specifier. %d simply instructs strftime to only output the day of a date.

Categories