Reputation: 61
I am exploring some data using pandas (I imported the dataset from excel using read_excel).
One of my columns is a datetime... How can I select the 'max' and 'min' for this datetime column?
This is the same question as here:
Getting min and max Dates from a pandas dataframe
But, it wasn't answered there... My datetime column is not my index (I just have the default index 0,1,2,3...)
I have tried using max(df['creation_date'])
but I dont think its recognizing a date, I get
'31-10-2020 22:33:10'
(In the dataset I have datetime from 2021 too...)
How can I select the max and min date? do I have to make my date to be my index in order to achieve it?
(I come from a sql background... When I do df.info() the Dtype is object for the datetime, can I change the data type of the column of a dataframe to datetime?)
Upvotes: 5
Views: 16786
Reputation: 24322
just change your 'creation_date' column from object
to datetime
dtype by:-
df['creation_date']=pd.to_datetime(df['creation_date'])
Now just calculate min and max dates value by:-
df['creation_date'].max()
df['creation_date'].min()
Note:- You can also convert your date like columns in datetime
while importing just by passing the list
of columns which have Date like Format in parse_date
parameter in pd.read_excel()
method
Example:-
df=pd.read_excel('your file.xlsx',engine='openpyxl',parse_dates=['creation_date'])
Upvotes: 7