(May-06-2022, 07:45 AM)Pedroski55 Wrote: As far as I know, you can compare dates directly。Not using pandas,can compare but most make the string to a datetime object first,or will just compare strings.
Assuming your data is in ascending order, you can find the position where you want to cut off an take a slice.
Also using pendulum make it easier as can parse automatically to a datetime object.
That also will handle time zones correctly.
>>> import pendulum >>> >>> x = '2022-04-25 7:07 PM' >>> y = '2022-04-25 7:08 AM' >>> x_date = pendulum.parse(x, strict=False) >>> y_date = pendulum.parse(y, strict=False) >>> x_date DateTime(2022, 4, 25, 19, 7, 0, tzinfo=Timezone('UTC')) >>> >>> x_date < y_date False >>> x_date.diff_for_humans(y_date) '11 hours after'If using pandas most also remember to covert dates to Pandas datetime.
Example:
>>> import pandas as pd >>> >>> d = {'Dates': ['2022-04-25 7:07 PM', '2022-04-25 7:08 AM'], 'Date_value': [111, 999]} >>> df = pandas.DataFrame(d) >>> df Dates Date_value 0 2022-04-25 7:07 PM 111 1 2022-04-25 7:08 AM 999 >>> >>> df.dtypes Dates object Date_value int64 dtype: object >>> >>> # Convert column to Pandas datetime64 >>> df['Dates'] = pd.to_datetime(df['Dates']) >>> df.dtypes Dates datetime64[ns] Date_value int64 dtype: object