<jupyter_start><jupyter_text>Question [[link](https://stackoverflow.com/questions/65766669/counting-amount-of-occurrences-within-a-certain-frame-in-timestamps-on-pandas)] Counting amount of occurrences within a certain frame in timestamps on Pandas I have a file (txt) containing fake data with the following 3 columns:---```user_13 visit_19    1330760979user_14 visit_20    1330732782user_14 visit_21    1330769600user_14 visit_22    1330783341user_14 visit_23    1330796012user_14 visit_24    1330797842```---Using Pandas, how can I find out from the time stamps on the third column, the amount of users who visited the page between 16:00 - 22:00?One user might have more than one visit, in order to qualify to the list, the first and last visit must have been between 16:00 and 22:00. Answer<jupyter_code>import pandas as pd
from io import StringIO
import datetime
%pprint
data = '''
user_13 visit_19    1330760979

user_14 visit_20    1330732782

user_14 visit_21    1330769600

user_14 visit_22    1330783341

user_14 visit_23    1330796012

user_14 visit_24    1330797842
'''
data = data.strip()      # remove trailing white-spaces and newline characters
data = io.StringIO(data) # create in-memory stream from the data string variable for use in read_csv
df = pd.read_csv(
     data,                              # the data in "CSV" format
     delim_whitespace=True,             # override default delimiter "," with whitespace
     names=['user','visit','timestamp'] # define column names for the resulting dataframe
)

df.values
df['timestamp'] = pd.to_datetime(df['timestamp'], # replace "timestamp" column values with datetime equivalent 
                                 unit='s')        # with up to second precision
lb = 16 # lower hour bound
ub = 22 # upper hour bound
vb = df[
     (lb <= df['timestamp'].dt.hour) # compare hour in "timestamp" column to lb using dt accessor
     &                               # perform logical and series operator on the two resulting series
     (ub >= df['timestamp'].dt.hour) # compare hour in "timestamp" column to lb using dt accessor
]

vb.values
print(vb.user.drop_duplicates().value_counts())<jupyter_output>user_14    1
Name: user, dtype: int64
{'fraction_non_alphanumeric': 0.07842248413417952, 'fraction_numerical': 0.09156844968268359, 'mean_word_length': 3.4585858585858587, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '567492', 'license': 'MIT', 'n_tokens_mistral': 772, 'n_tokens_neox': 639, 'n_words': 233, 'path': 'pandas/stackoverflow-65766669-counting-amount-of-occurrences-within-a-certain-frame-in-timestamps-on-pandas.ipynb', 'repo_name': 'jdsalaro/snippets', 'size': '2206'}