1

So the picture is the data set I'm dealing with. I'm using Python 3 and Pandas pack. Question I'm answering is: "What was the month and day of the first sale? Store as a tuple in that order and assign the tuple to the variable first_date."
enter image description here

My thought process was turning the 'trans_timestamp' column into int rather than floats so I could find the min value of the int. This is the code I implemented

df_cleaned['trans_timestamp'] = df_cleaned['trans_timestamp'].astype(int)

first_date = df_cleaned['trans_timestamp'].min()

first_date

I'm guessing either the .astype(int) is the problem, or I'm just looking at the problem in a completely different perspective than how they want me too. Anything helps thanks!

blackraven
  • 5,284
  • 7
  • 19
  • 45
  • [Why should I not upload images of ... when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question). [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). [Why not upload images of code on SO ...?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). [You should not post code as an image because:...](https://meta.stackoverflow.com/a/285557/2823755) – wwii Sep 12 '22 at 23:19
  • [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Sep 12 '22 at 23:19
  • Please format the code - select it and type `ctrl-k`. .. [Formatting help](https://stackoverflow.com/editing-help)... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Sep 12 '22 at 23:20
  • Why isn't the `'trans_timestamp'` a [datetime Series](https://pandas.pydata.org/docs/user_guide/timeseries.html)? – wwii Sep 12 '22 at 23:23
  • Hi wwii thanks for pointing this out and editing it to community standards. Next question I'll be sure to add a mock dataset, and not add any screenshots. I have no clue about the datetime series, this is my first time hearing of them; and even still I have no clue how to convert the data in the column to it. – Ellendejefferes Sep 12 '22 at 23:34
  • Did you explore the Time series functionality link to the Pandas User Guide that I put in my previous comment? If your going to be using Pandas in the future it would serve you well to spend plenty of time working your way through that [User Guide](https://pandas.pydata.org/docs/user_guide/index.html), starting at the beginning - 10 minutes to pandas . – wwii Sep 13 '22 at 13:43

1 Answers1

0

The column trans_timestamp could be in string format instead of datetime object. Use pd.to_datetime() to convert to datetime format:

import pandas as pd

df = pd.DataFrame({'trans_timestamp': ['2021-01-01 07:35:21.439','2021-01-01 09:33:37.499','2021-06-30 15:37:12.821'],
                   'prod_price': ['72.99','18.96','9.95']  })
df['trans_timestamp'] = pd.to_datetime(df['trans_timestamp'])
print(df)

          trans_timestamp prod_price
0 2021-01-01 07:35:21.439      72.99
1 2021-01-01 09:33:37.499      18.96
2 2021-06-30 15:37:12.821       9.95

To check if the column is in datatime format, use df['trans_timestamp'].info(), you'd get datetime64[ns]. Then you can use .min() to get the smallest date

print(df['trans_timestamp'].min())
#output: Timestamp('2021-01-01 07:35:21.439000')

ts = df['trans_timestamp'].min()
first_date = (ts.month, ts.day)
print(first_date)
#output: (1, 1)
blackraven
  • 5,284
  • 7
  • 19
  • 45