1

Beginner here and a little confused.

I have a set of data that makes heavy use of UNIX int timestamps (ms). A lot of this is outside the scope of my software, we're talking APIs, DBs, etc. etc.

These APIs, Databases, etc. Are all setup to ingest and return Dataframes, and these contain the dates as ints: 1642463640000

Because of this, I don't want to convert these to readable dates.

In addition to this these timestamps are set as an index, so for example:

df = pd.DataFrame({"timestamp": 1642463640000, 'otherdata':'yolo'})
df.set_index('timestamp', inplace=True)

My problem is that I can't, for the life of me, figure out how to slice this data to get, say, everything between two dates.

Because everything is expecting timestamps, I don't want to convert from timestamp to date and back constantly. Am I missing something obvious? Is there a timestamp dtype maybe? Any help would be appreciated.

1
  • You could convert the date(s) to use for slicing to int timestamp instead. That should be only 2 conversions. Commented Jan 19, 2022 at 5:51

1 Answer 1

2

Agree with the suggestion by @mozway. I have used similar techniques before when working with unix epochs.

Consider this code:

import pandas as pd
from datetime import datetime

def timestamp(dt):
    epoch = datetime.utcfromtimestamp(0)
    return (dt - epoch).total_seconds() * 1000.0

dt_high = datetime(2022, 1, 18)
dt_low = datetime(2022, 1, 15)

epoch_high = timestamp(dt_high)
epoch_low = timestamp(dt_low)

filtered = df[(df.index > epoch_low) & (df.index < epoch_high)]
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work for me. Am I missing something? I get a KeyError: 'timestamp' when running the last line
I can get this working > df[(df["timestamp"] > epoch_low) & (df["timestamp"] < epoch_high)] IF I reset the index and turn timestamp into a column. Is there no better way?
You pushed me in the right direction. Got this working : filtered = df[(df.index > 1642463630000) & (df.index < 1642463650000)] Thank you!! Feel free to add that to your response and I'll approve it :)
Yes, the last line was untested because I did not have the right data frame. Fixed it now and retested using dates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.