How to use Pandas calculate # of weeks/days? [duplicate] - python

This question already has answers here:
Pandas Datetime: Calculate Number of Weeks Between Dates in Two Columns
(2 answers)
Closed 4 years ago.
Hi, I have a dataframe with date columns. I want to add a column to calculate how many weeks since the contact? For example, today's date is 20-Sep-18, and use this date to calculate with the column.
Can anyone help me with this questions? Thanks!

You can do like this.
df['Contact Date']= pd.to_datetime(df['Contact Date'])
import datetime
df['How Many days'] = datetime.datetime.now() - df['Contact Date']

Related

pandas calculate time difference to now [duplicate]

This question already has answers here:
Compare timestamp with datetime
(2 answers)
Timestamp String in Zulu Format To Datetime
(1 answer)
Closed 9 months ago.
I have a pandas dataframe df with a time column containing datetime values. I now want to filter the dataframe to show rows with time values lying in the next 15 minutes.
So first I try to simply subtract the current time from the datetimes.
df.Time = pd.to_datetime(df.Time)
print(df.Time - pd.to_datetime("today"))
But got this error:
TypeError: Cannot subtract tz-naive and tz-aware datetime-like objects
I tried to remove the tz-awareness with .replace(tzinfo=None) but it was not working. In the end I am looking for a command like this (assuming the difference of two datetimes in is minutes):
df.loc[df.Time - pd.to_datetime("today") < 15]

Read excel dates and convert them to individual strings [duplicate]

This question already has answers here:
Extract day and month from a datetime object
(4 answers)
Closed 12 months ago.
I recently started using python.
I have a series of dates in excel
01-05-2021
02-05-2021
.
.
29-05-2021
Now, I want to load this column and convert it into individual strings based on rows. So i can extract the day, month and year separately for each dates
Can someone help me how to do that??
you can do:
df = pd.read_excel("filename.xlsx")
# let's imagine your date column name is "date"
df["day"] = df["date"].apply(lambda elem:elem.split("-")[0])
df["month"] = df["date"].apply(lambda elem:elem.split("-")[1])
df["year"] = df["date"].apply(lambda elem:elem.split("-")[2])
from datetime import datetime
str_time = 01-05-2021
time_convert = datetime.strptime(str_time, '%d-%m-%Y')
print (time_convert, time_convert.day, time_convert.month, time_convert.year)
in your case, make the convert in looping for each data you got from the excel file

Remove "days 00:00:00"from dataframe [duplicate]

This question already has answers here:
Pandas Timedelta in Days
(5 answers)
Closed 3 years ago.
So, I have a pandas dataframe with a lot of variables including start/end date of loans.
I subtract these two in order to get their difference in days.
The result I get is of the type i.e. 349 days 00:00:00.
How can I keep only for example the number 349 from this column?
Check this format,
df['date'] = pd.to_timedelta(df['date'], errors='coerce').days
also, check .normalize() function in pandas.

numpy datetime64 from day, month, year series [duplicate]

This question already has answers here:
Vectorized year/month/day operations with NumPy datetime64
(3 answers)
Closed 3 years ago.
I have dates are three numpy arrays containing each all the days, months or years separately.
From these date-components I would like to construct a numpy.datetime64 array:
date = np.datetime64(days, months, years)
Of course the above does not work. The numpy documentation is silent on how to parse dates from anything other than strings.
I am sure somebody has already solved this riddle before...
First Convert to date time like
from datetime import datetime
dt = datetime(year, month, date)
then
date = np.datetime64(dt)

day counter using pandas [duplicate]

This question already has answers here:
Add a sequential counter column on groups to a pandas dataframe
(4 answers)
Closed 4 years ago.
if I have a data set of time series and I want to estimate the number of the day of a groupby time series per each day as seen in the figure and act as a counter :
nothing special in my code yet, it is just reading the data and convert time and day into
import pandas as pd
df = pd.read_csv('*file location and name*',sep=",")
df.head()
df['Date'] =pd.to_datetime(df['Date']+" "+df['Time'])
df.set_index('Date', inplace=True)
See if answers your query:
df['dayOfMonth']= df.groupby('day').cumcount() + 1

Categories