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)
Related
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]
This question already has answers here:
datetime to string with series in pandas
(3 answers)
Closed 2 years ago.
I'm converting a datetime column (referred to as DATE) in my Pandas dataframe df to a string of the form 'Ymd' (e.g. '20191201' for December 1st 2019). My current way of doing that is:
import datetime as dt
df['DATE'] = df['DATE'].apply(lambda x: dt.datetime.strftime(x, '%Y%m%d'))
But this is surprisingly inefficient and slow when run on large dataframes with millions of rows. Is there a more efficient alternative I am not seeing? That would be extremely helpful. Thanks.
In pandas you do not need apply
df['Date']=df['DATE'].dt.strftime('%Y%m%d')
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.
This question already has answers here:
How to compare two dates?
(6 answers)
Closed 3 years ago.
i want to compare/subtract two date's due date from current date in python and apply a condition on this subtraction that if the difference is >0 days then calculate fine by multiplying difference with fine per day
from datetime import date
a=date.today()
issuedate=date(2019,5,9)
duedate#should be 5 days after issue date i can't find the method for doing this
check=a-duedate
# if check>0days:
# print(check days*40)
You need to use the timedelta function in the datetime module:
from datetime import date
import datetime
a=date.today()
issuedate=date(2019,5,9)
duedate = issuedate+datetime.timedelta(days=5)
check=a-duedate
print(check>=datetime.timedelta(days=0))
Use timedelta
from datetime import timedelta
duedate = issuedate + timedelta(days=5)
check=(a-duedate).days
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']