Python: converting timestamp to date time not working

Question:

I am requesting data from the api.etherscan.io website. For this, I require a free API key. I am getting information for the following wallet addresses 0xdafea492d9c6733ae3d56b7ed1adb60692c98bc5, 0xc508dbe4866528db024fb126e0eb97595668c288. Below is the code I am using:

wallet_addresses = ['0xdafea492d9c6733ae3d56b7ed1adb60692c98bc5', '0xc508dbe4866528db024fb126e0eb97595668c288']

page_number = 0
df_main = pd.DataFrame()
while True:
    for address in wallet_addresses:
        url=f'https://api.etherscan.io/api?module=account&action=txlist&address={address}&startblock=0&endblock=99999999&page={page_number}&offset=10&sort=asc&apikey={ether_api}'
        output = requests.get(url).text
        df_temp = pd.DataFrame(json.loads(output)['result'])
        df_temp['wallet_address'] = address
        df_main = df_main.append(df_temp)
        page_number += 1
        df_main['timeStamp'] = pd.to_datetime(df_main['timeStamp'], unit='s')
        if min(pd.to_datetime(df_main['timeStamp']).dt.date) < datetime.date(2022, 1, 1):
            pass

Note that you need your own (free) ether_api.

What I want to do is get data from today’s date, all the way back to 2022-01-01 which is what I am trying to achieve in the if statement.

However, the above gives me an error: ValueError: unit='s' not valid with non-numerical val='2022-09-19 18:14:47'

How can this be done? I’ve tried multiple methods to get pandas datetime to work, but all of them gave me errors.

Asked By: MathMan 99

||

Answers:

Here you go, it’s working without an error:

page_number = 0
df_main = pd.DataFrame()
while True:
    for address in wallet_addresses:
        url=f'https://api.etherscan.io/api?module=account&action=txlist&address={address}&startblock=0&endblock=99999999&page={page_number}&offset=10&sort=asc&apikey={ether_api}'
        output = requests.get(url).text
        df_temp = pd.DataFrame(json.loads(output)['result'])
        df_temp['wallet_address'] = address
        page_number += 1
        df_temp['timeStamp'] = pd.to_datetime(df_temp['timeStamp'], unit='s')
        df_main = df_main.append(df_temp)
        if min(pd.to_datetime(df_main['timeStamp']).dt.date) < datetime(2022, 1, 1).date():
            pass

I’ll provide this answer with proper explanation a bit later. As for now, please see the comments.

Answered By: DiMithras