How to calculate the difference between dates in python [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I subtract a day from a python date?
subtract two times in python
I had generated date in python as below
import time
time_stamp = time.strftime('%Y-%m-%d')
print time_stamp
Result:
'2012-12-19'
What i am trying is, if above time_stamp is the present today's date , i want a date '2012-12-17' by performing difference(substraction of two days)
So how to perform date reduction of two days from the current date in python

To perform calculations between some dates in Python use the timedelta class from the datetime module.
To do what you want to achieve, the following code should suffice.
import datetime
time_stamp = datetime.datetime(day=21, month=12, year=2012)
difference = time_stamp - datetime.timedelta(day=2)
print '%s-%s-%s' % (difference.year, difference.year, difference.day)
Explanation of the above:
The second line creates a new datetime object (from the the datetime class), with specified day, month and year
The third line creates a timedelta object, which is used to perform calculations between datetime objects

Related

How to convert a column in pandas data frame as Time object [duplicate]

This question already has answers here:
Pandas - convert strings to time without date
(3 answers)
Closed 1 year ago.
I have csv file with the date and jobs runtime values, I need to convert the object to time format.
Time value will be as follows:
00:04:23
00:04:25
pd.to_datetime(df[‘Time’], format=‘%H:%M:%S:’)
This returns the value with default date
1900-01-01 00:04:23
1900-01-01 00:04:25
How do I retain only the runtime as time data type in the column without date.
We can make use of to_timedelta function in pandas.
df['Time'] = pd.to_timedelta(df['Time'])
It will create time format of `timedelta64[ns]
You can use pandas.Series.dt.time() to access time part.
print(pd.to_datetime(df['Time'], format='%H:%M:%S').dt.time)
'''
0 00:04:23
1 00:04:25
Name: Time, dtype: object
'''

calculate the difference of two timestamp columns [duplicate]

This question already has answers here:
Calculate Time Difference Between Two Pandas Columns in Hours and Minutes
(4 answers)
calculate the time difference between two consecutive rows in pandas
(2 answers)
Closed 2 years ago.
I have a dataset like this:
data = pd.DataFrame({'order_date-time':['2017-09-13 08:59:02', '2017-06-28 11:52:20', '2018-05-18 10:25:53', '2017-08-01 18:38:42', '2017-08-10 21:48:40','2017-07-27 15:11:51',
'2018-03-18 21:00:44','2017-08-05 16:59:05', '2017-08-05 16:59:05','2017-06-05 12:22:19'],
'delivery_date_time':['2017-09-20 23:43:48', '2017-07-13 20:39:29','2018-06-04 18:34:26','2017-08-09 21:26:33','2017-08-24 20:04:21','2017-08-31 20:19:52',
'2018-03-28 21:57:44','2017-08-14 18:13:03','2017-08-14 18:13:03','2017-06-26 13:52:03']})
I want to calculate the time differences between these dates as the number of days and add it to the table as the delivery delay column. But I need to include both day and time for this calculation
for example, if the difference is 7 days 14:44:46 we can round this to 7 days.
from datetime import datetime
datetime.strptime(date_string, format)
you could use this to convert the string to DateTime format and put it in variable and then calculate it
Visit https://www.journaldev.com/23365/python-string-to-datetime-strptime/
Python's datetime library is good to work with individual timestamps. If you have your data in a pandas DataFrame as in your case, however, you should use pandas datetime functionality.
To convert a column with timestamps from stings to proper datetime format you can use pandas.to_datetime():
data['order_date_time'] = pd.to_datetime(data['order_date_time'], format="%Y-%m-%d %H:%M:%S")
data['delivery_date_time'] = pd.to_datetime(data['delivery_date_time'], format="%Y-%m-%d %H:%M:%S")
The format argument is optional, but I think it is a good idea to always use it to make sure your datetime format is not "interpreted" incorrectly. It also makes the process much faster on large data-sets.
Once you have the columns in a datetime format you can simply calculate the timedelta between them:
data['delay'] = data['delivery_date_time'] - data['order_date_time']
And then finally, if you want to round this timedelta, then pandas has again the right method for this:
data['approx_delay'] = data['delay'].dt.round('d')
where the extra dt gives access to datetime specific methods, the round function takes a frequency as arguments, and in this case that frequency has been set to a day using 'd'

How can I calculate number of days between two dates? [duplicate]

This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 3 years ago.
If I have two dates (ex. 19960104 and 19960314), what is the best way to get the number of days between these two dates?
Actually I have to calculate many dates in my dataframe. I use the code:
`for j in range(datenumbers[i]):
date_format = "%Y%m%d"
a = datetime.strptime(str(df_first_day.date[j]), date_format)
b = datetime.strptime(str(df_first_day.exdate[j]), date_format)
delta = (b - a).days
df_first_day.days_to_expire[j] = delta`
I need to put every difference between two dates in one of column of my dataframe. I wonder if there is a better way to do as not using for loop
You will find dates much easier to handle if you first convert text to datetime.
Then it becomes trivial to compute a timedelta that answers your question.
import datetime as dt
fmt = '%Y%m%d'
d1 = dt.datetime.strptime('19960104', fmt)
d2 = dt.datetime.strptime('19960314', fmt)
print((d2 - d1).days)
This displays:
70
EDIT
If you choose to define a function:
def num_to_timestamp(ymd):
return dt.datetime.strptime(str(num), '%Y%m%d')
then you can conveniently apply it to a column:
df['date'] = df['date'].apply(num_to_timestamp)
Similarly the axis=1 option lets you construct a column that is difference of two existing columns.

How can I convert a timestamp string of the form "%d%H%MZ" to a datetime object?

I have timestamp strings of the form "091250Z", where the first two numbers are the date and the last four numbers are the hours and minutes. The "Z" indicates UTC. Assuming the timestamp corresponds to the current month and year, how can this string be converted reliably to a datetime object?
I have the parsing to a timedelta sorted, but the task quickly becomes nontrivial when going further and I'm not sure how to proceed:
datetime.strptime("091250Z", "%d%H%MZ")
What you need is to replace the year and month of your existing datetime object.
your_datetime_obj = datetime.strptime("091250Z", "%d%H%MZ")
new_datetime_obj = your_datetime_obj.replace(year=datetime.now().year, month=datetime.now().month)
Like this? You've basically already done it, you just needed to assign it a variable
from datetime import datetime
dt = datetime.strptime('091250Z', '%m%H%MZ')

Is there an equivalent for dateutil relativedelta(weekday=FR) but for MONTHS?

I would like to parse dates in a script and I was wondering if there is an equivalent of the:
relativedelta(days=1, weekday=MO)
but for months?
For now, I extract the month number in my text and compare it to the document's creation date (and month). However, this is quite long and repetitive (and I have to do it for future tenses, present tenses and past tenses)...
Adding relativedelta(month=2) to a datetime object will give you the same date and time, except in February. If this creates a non-existent date, the date will be truncated at the last existing date, e.g.:
from datetime import datetime
from dateutil.relativedelta import relativedelta
print(datetime(2015, 3, 30) + relativedelta(month=2)) # 2015-02-28 00:00:00
As explained in the relativedelta documentation:
year, month, day, hour, minute, second, microsecond:
Absolute information (argument is singular); adding or subtracting a
relativedelta with absolute information does not perform an aritmetic
operation, but rather REPLACES the corresponding value in the
original datetime with the value(s) in relativedelta.
All the "singular" arguments are processed as "set this component of the thing I'm being added to/subtracted from to this value", whereas the plural versions of the same arguments say "Add/subtract this number to/from this component".
Note that the relativedelta documentation also lays out the order that each component is applied, but suffice to say that absolute values are applied before relative values, so relativedelta(month=3, months=2) will set the month to March, then add 2 months (so, basically, it's equivalent to relativedelta(month=5)).
The equivalent of weekday = MO (or weekday = calendar.MONDAY) can be month = 1 for January, month = 2 for February, and so on.
These operations are handled most gracefully by the Python arrow library. Datetime objects can be converted to Arrow objects and vice versus if you need to continue using datetime, i.e. for compatibility with other modules.

Categories

Resources