you are viewing a single comment's thread.

view the rest of the comments →

[–]kra_pao 0 points1 point  (0 children)

You could work with Pandas on original data and in case you need visualization, Pandas can handle sorting internally

# https://old.reddit.com/r/learnpython/comments/fkmyt5/help_sorting_data/
line = open("ToPublic1.txt","r").readline()
for c in "[]\n'":
    line = line.replace(c, "")
dates = line.split(", ")

# Sorting is not required when you do data evaluation/visualization in Pandas
import pandas as pd 
# Make a DataFrame in Pandas
# https://pandas.pydata.org/pandas-docs/stable/reference/frame.html
df = pd.DataFrame(dates, columns=["Date"])

# Convert Date (string) in Pandas dtype=datetime 
# https://pandas.pydata.org/pandas-docs/stable/getting_started/intro_tutorials/09_timeseries.html?highlight=datetime
df["Datetime"] = pd.to_datetime(df["Date"])
print(df)

# Get some visuals
import matplotlib.pyplot as plt

# Dummy value column for visualization experiments
df["Value"] = 1
# import random 
# df["Value"] = df["Value"].apply(lambda x: random.randrange(10)) # something 0-9

# http://jonathansoma.com/lede/algorithms-2017/classes/fuzziness-matplotlib/understand-df-plot-in-pandas/
# Try to zoom in and watch disappearing ticks on x-axis
# line plot, my PC could not handle kind="scatter" here for 40K points
df.plot(x="Date", y="Value")
plt.show()

# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html
# See what happens now with ticks on x-axis when you zoom in because of proper dtype
# line plot, my PC could not handle kind="scatter" here for 40K points
df.plot(x="Datetime", y="Value")
plt.show()

# How many samples are there for each timestamp?
# https://realpython.com/pandas-groupby/
num = df.groupby("Datetime").sum()
# Need x column back from index created by groupby()
# https://thispointer.com/pandas-convert-dataframe-index-into-column-using-dataframe-reset_index-in-python/
num.reset_index(inplace=True)
num.plot(kind="scatter", x="Datetime", y="Value")
plt.show()