4

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?)

francmarian
  • 51
  • 1
  • 1
  • 2
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. In particular, check the type of your column. No, it doesn't have to be the index, but it *does* have to be a type with an ordering function (such as `<`) defined with the semantics you need. `datetime` is a good choice. – Prune Feb 28 '21 at 18:11
  • [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. – Prune Feb 28 '21 at 18:11

1 Answers1

5

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'])
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41