Home > Enterprise >  python | arrow.get error and replace serial number with real time
python | arrow.get error and replace serial number with real time

Time:11-17

Below is stock price data and I save it into a DataFrame. Column t is a serial number that represents time.

a b t
1.20 5.45 1636534800000
7.98 1.33 1636542000000
8.29 2.44 1636549200000
8.76 6.55 1636556400000
  1. if I put arrow.get(1636534800000), then I would get 2021-11-10T09:00:00 00:00 without no issue

However, I got an error when doing the below. be noted that the data type for t column is int64

trade_date = df.loc[0,'t'] 
date_reformat=arrow.get(trading_date)

error message:

"raise TypeError(f"Cannot parse single argument of type {type(arg)!r}.") TypeError: Cannot parse single argument of type <class 'numpy.int64'>."

  1. I need a code to replace the value in t column with "real time" instead of serial number. The value in t column is in UTC time zone and I live in east coast U.S, so I need it to adjust back to eastern time. However we have day light saving. We are currently in EST timezone, and after march we gonna go to EDT timezone. So if the time is between Mar and Nov then t should be adjusted to UTC-4, and rest of the time the t column should be adjusted to UTC-5. I don't need the " 00:00", I only need that year-month-dateThh:mm:ss

CodePudding user response:

Your input is UNIX time in milliseconds (since 1970-01-01 UTC). Convert to datetime using

df['datetime'] = pd.to_datetime(df['t'], unit='ms', utc=True)

Setting utc=True will specify that the date/time is in UTC (not local time etc.). Now you can convert to US/Eastern time zone to account for DST active/inactive:

df['datetime'] = df['datetime'].dt.tz_convert('America/New_York')

and convert to string with a certain format, e.g. not showing the UTC offset:

df['datetime_string'] = df['datetime'].dt.strftime('%Y-%m-%d %H:%M:%S')

which gives you for the example:

df[['datetime', 'datetime_string']]

                   datetime      datetime_string
0 2021-11-10 04:00:00-05:00  2021-11-10 04:00:00
1 2021-11-10 06:00:00-05:00  2021-11-10 06:00:00
2 2021-11-10 08:00:00-05:00  2021-11-10 08:00:00
3 2021-11-10 10:00:00-05:00  2021-11-10 10:00:00
  • Related