Chapter 2 - Filtering data

In the previous chapter, you learned how to read and print data that is a bit raw. Now, try to select a few columns and handle them properly.

Start with these two columns: Time (time) and Magnitude (mag). After getting the information from these columns, filter and adapt the data. Try formatting the date to Qt types.

There is not much to do for the Magnitude column, as it’s just a floating point number. You could take special care to check if the data is correct. This could be done by filtering the data that follows the condition, “magnitude > 0”, to avoid faulty data or unexpected behavior.

The Date column provides data in UTC format (for example, 2018-12-11T21:14:44.682Z), so you could easily map it to a class:~PySide6.QtCore.QDateTime object defining the structure of the string. Additionally, you can adapt the time based on the timezone you are in, using QTimeZone.

The following script filters and formats the CSV data as described earlier:

 1import argparse
 2import pandas as pd
 3
 4from PySide6.QtCore import QDateTime, QTimeZone
 5
 6
 7def transform_date(utc, timezone=None):
 8    utc_fmt = "yyyy-MM-ddTHH:mm:ss.zzzZ"
 9    new_date = QDateTime().fromString(utc, utc_fmt)
10    if timezone:
11        new_date.setTimeZone(timezone)
12    return new_date
13
14
15def read_data(fname):
16    # Read the CSV content
17    df = pd.read_csv(fname)
18
19    # Remove wrong magnitudes
20    df = df.drop(df[df.mag < 0].index)
21    magnitudes = df["mag"]
22
23    # My local timezone
24    timezone = QTimeZone(b"Europe/Berlin")
25
26    # Get timestamp transformed to our timezone
27    times = df["time"].apply(lambda x: transform_date(x, timezone))
28
29    return times, magnitudes
30
31
32if __name__ == "__main__":
33    options = argparse.ArgumentParser()
34    options.add_argument("-f", "--file", type=str, required=True)
35    args = options.parse_args()
36    data = read_data(args.file)
37    print(data)

Now that you have a tuple of QDateTime and float data, try improving the output further. That’s what you’ll learn in the following chapters.