Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pandas to_datetime with timestamps

I'm trying to covert my these timestamps into a %Y-%m-%d %H:%M format. Here's a sample of the data:

0    1450753200
1    1450756800
2    1450760400
3    1450764000
4    1450767600
Name: ohlcv_start_date, dtype: int64

Could someone explain what type of timestamp these are and what code I need to convert them properly, because when I use:

pd.to_datetime(df[TS], unit='ms').dt.strftime('%Y-%m-%d %H:%M')

It converts the time into:

0        1970-01-01 00:00
1        1970-01-01 00:00
2        1970-01-01 00:00
3        1970-01-01 00:00
4        1970-01-01 00:00

Which isn't correct

EDIT: Thanks Mr Chum.

What i'm actually trying to do is merge the values of different assets by timestamp. Each asset starts and finishes at slightly different times and Upon analysis it seems there is gaps in the data:

 market_trading_pair  next_future_timestep_return ohlcv_start_date  \
0        Poloniex_ETH_BTC                 3.013303e-03    2015-12-22 03      
1        Poloniex_ETH_BTC                 3.171481e-03    2015-12-22 05   
2        Poloniex_ETH_BTC                -1.381575e-03    2015-12-22 07   
3        Poloniex_ETH_BTC                -4.327704e-03    2015-12-22 08   

The best I can think to solve this problem is to create a new data frame and fill in the rows with time stamps incrementing by one hours, from here i can simple merge in the asset data. Any idea how to generate ascending timstamps ?

like image 430
David Hancock Avatar asked Feb 10 '16 10:02

David Hancock


People also ask

How do I compare timestamps in pandas?

Comparison between pandas timestamp objects is carried out using simple comparison operators: >, <,==,< = , >=. The difference can be calculated using a simple '–' operator. Given time can be converted to pandas timestamp using pandas. Timestamp() method.

How do you convert a Timestamp to a DataFrame in Python?

read_csv(), the timestamps column from the data Dataframe is given as an argument in the to_datetime() for it to be converted into DateTime. unit='s' is used to convert the values of the timestamp column to epoch time after converting the values to DateTime it is stored in a column called 'Datetime' in the Dataframe.

Is pandas Timestamp the same as datetime?

Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.

How do pandas deal with date time?

Pandas has a built-in function called to_datetime()that converts date and time in string format to a DateTime object. As you can see, the 'date' column in the DataFrame is currently of a string-type object. Thus, to_datetime() converts the column to a series of the appropriate datetime64 dtype.

How do I convert a timestamp to a datetime in pandas Dataframe?

You can use the following basic syntax to convert a timestamp to a datetime in a pandas DataFrame: timestamp. to_pydatetime () The following examples show how to use this function in practice. Example 1: Convert a Single Timestamp to a Datetime. The following code shows how to convert a single timestamp to a datetime:

How to generate a range of timestamps from a date in Python?

In this approach, we directly use the date_range () method from the panda’s library. The start date is datetime. today () which is today’s date. Periods is the number of periods to generate. We can directly generate a range of timestamps by using this method.

What is pandas datetime64?

Pandas utilizes NumPy’s datetime64 dtype consolidated with the Standard Library’s datetime and scikit learn timeseries object to provide the functionality to date series — and these have also been expanded with new functionalities by Pandas ( please follow this link for the documentation ).

How to assemble a datetime from multiple columns of a Dataframe?

Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) or plurals of the same If a date does not meet the timestamp limitations, passing errors=’ignore’ will return the original input instead of raising any exception.


1 Answers

Pass unit='s' to get the values as it's epoch time:

In [106]:
pd.to_datetime(df['timestamp'], unit='s')
Out[106]:
index
0   2015-12-22 03:00:00
1   2015-12-22 04:00:00
2   2015-12-22 05:00:00
3   2015-12-22 06:00:00
4   2015-12-22 07:00:00
Name: timestamp, dtype: datetime64[ns]

You can convert to string if you desire:

In [107]:

pd.to_datetime(df['timestamp'], unit='s').dt.strftime('%Y-%m-%d %H:%M')
Out[107]:
index
0    2015-12-22 03:00
1    2015-12-22 04:00
2    2015-12-22 05:00
3    2015-12-22 06:00
4    2015-12-22 07:00
Name: timestamp, dtype: object
like image 109
EdChum Avatar answered Nov 02 '22 23:11

EdChum