0

Here's my data

id enter_time
1  1.481044e+12
2  1.486d74e+12

Here's my expected output

id enter_time     enter_timestamp
1  1.481044e+12   2017-07-14 08:10:03
2  1.486774e+12   2017-07-15 08:10:00

Note: value in "enter_timestamp" in expectation above is false in value, it's only for give idea about expected format, below is additional information just in case needed for answer

In : main['enter_time'].dtype
Out: dtype('float64')

What I already try

import datetime
main['loan_timestamp'] = datetime.datetime.fromtimestamp(main['loan_time'])

and the output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-cfe2d514debb> in <module>
      1 import datetime
----> 2 main['loan_timestamp'] = datetime.datetime.fromtimestamp(main['loan_time'])

/opt/conda/lib/python3.8/site-packages/pandas/core/series.py in wrapper(self)
    127         if len(self) == 1:
    128             return converter(self.iloc[0])
--> 129         raise TypeError(f"cannot convert the series to {converter}")
    130 
    131     wrapper.__name__ = f"__{converter.__name__}__"

TypeError: cannot convert the series to <class 'int'>

Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70
  • 1
    Does this answer your question? [Convert unix time to readable date in pandas dataframe](https://stackoverflow.com/questions/19231871/convert-unix-time-to-readable-date-in-pandas-dataframe) – FObersteiner Feb 17 '21 at 11:20
  • Yes @MrFuppes, it answers In [25], but I don't know that it come frome unix time before – Nabih Bawazir Feb 18 '21 at 01:29
  • no problem, I just added the link so that other people coming here can easily find related resources. – FObersteiner Feb 18 '21 at 09:01

1 Answers1

2

Try using pandas to_datetime() (I assumed that the character 'd' in your second input float is a typo, so I replaced it):

import pandas as pd

df = pd.DataFrame([(1, 1.481044e+12), (2, 1.48674e+12)], columns=['id', 'enter_time'])
df['enter_timestamp'] = pd.to_datetime(df['enter_time'], unit='ms')
df

    id  enter_time  enter_timestamp
0   1   1.481044e+12    2016-12-06 17:06:40
1   2   1.486740e+12    2017-02-10 15:20:00

I was assuming that the unit of enter_time is in milliseconds, as seconds or higher will result in an OutOfBoundsError (fundamental limitation of pandas timestamp).

user14893379
  • 115
  • 8