0

I have tried many ways to have this done, but I'm still getting errors. What I am doing wrong?

If I attempt to get this done with just simple string, everything goes ok:

import pytz
from datetime import datetime

id_board = '608332c0'
df_final = datetime.fromtimestamp(int(id_board,16))
print(df_final)
output: 2021-04-23 14:49:04

However, I do have a list I would like to get the datestamp, here is where the code that gives me errors:

import pytz
import pandas as pd
from datetime import datetime

df_final = pd.read_csv("Book1.csv")
print(df_final)

                    Card ID  # of Members
0  608332c0806da55df13b498b             2
1  60819c7ccd3a695b79f54f53             5
2  60817b9df8f5422bbaf2cff9             9
3  60806f3d24f11404f1470904             2
4  607ed78a89de73411e937655             1
5  608332e6943e7263a56fb3ff             2

x = df_final["Card ID"].str[0:8]
df_final["Created Date"] = x
print(df_final)

                   Card ID  # of Members Created Date
0  608332c0806da55df13b498b             2     608332c0
1  60819c7ccd3a695b79f54f53             5     60819c7c
2  60817b9df8f5422bbaf2cff9             9     60817b9d
3  60806f3d24f11404f1470904             2     60806f3d
4  607ed78a89de73411e937655             1     607ed78a
5  608332e6943e7263a56fb3ff             2     608332e6

df_final["Created Date"] = datetime.fromtimestamp(int(x,16))
print(df_final)

Traceback (most recent call last):
  File "c:\Users\xxxxx\Desktop\Manual Formulas 1\hexa.py", line 17, in <module>
    df_final["Created Date"] = datetime.fromtimestamp(int(x,16))
TypeError: int() can't convert non-string with explicit base

I have tried many "fixes" I have read but still the same. I also tried with a loop over the rows but got the following error:

invalid literal for int() with base 16

Thanks for your help! Joe.

Robert Davy
  • 866
  • 5
  • 13
Joe_Ugalde
  • 21
  • 4
  • Does this help at all? https://stackoverflow.com/questions/37955856/convert-pandas-dataframe-column-from-hex-string-to-int – Robert Davy May 06 '21 at 03:09

2 Answers2

0

I know that you think you are converting strings into integers.
However, you are not actually converting strings into integers.

Consider the following code:

my_favorite_number = int("AF91", 16)
print("THE FIRST CONVERSION WORKED\n")

my_favorite_number = int(8234, 16)
print("THE SECOND CONVERSION WORKED")  

The console output is the following:

THE FIRST CONVERSION WORKED

Traceback (most recent call last):
  File "D:/python_sandbox/fgdfgdf.py", line 6, in <module>
    my_favorite_number = int(8234, 16)
TypeError: int() can't convert non-string with explicit base

The key thing to notice is that int() can't convert non-string

Please convert your input into a string before converting it into an integer.

For example, you might write,

numby_the_number = int(str(datum), 16)
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
0

Thanks for taking the time to look into my question. I worked a bit more on the code and I was able to perform the action I was looking for. Here is the resolution for learning purposes:

from datetime import datetime    
df_final = pd.read_csv("Book1.csv")
    get_date = df_final["Card ID"].str[0:8].apply(lambda x: datetime.fromtimestamp(int(x,16)))
    df_final["Nueva"] = get_date
    print(df_final)
                    Card ID  # of Members               Nueva
0  608332c0806da55df13b498b             2 2021-04-23 14:49:04
1  60819c7ccd3a695b79f54f53             5 2021-04-22 09:55:40
2  60817b9df8f5422bbaf2cff9             9 2021-04-22 07:35:25
3  60806f3d24f11404f1470904             2 2021-04-21 12:30:21
4  607ed78a89de73411e937655             1 2021-04-20 07:30:50
5  608332e6943e7263a56fb3ff             2 2021-04-23 14:49:42
Joe_Ugalde
  • 21
  • 4