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 ?
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.
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.
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.
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.
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:
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.
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 ).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With