0

Please bear with me as I am quite new to python:

Currently here is my code:

import pandas as pd
import statistics
import matplotlib.pyplot as plt
import math

df = pd.read_csv(r"/Users/aaronhuang/Documents/Desktop/ffp/exfileCLEAN2.csv",
                 skiprows=[1])  # replace this with wherever the  file is.

magnitudes = df['Magnitude '].values
times = df['Time '].values
average = statistics.mean(magnitudes)
sd = statistics.stdev(magnitudes)
below = sd * 3


class data_set:
    def __init__(self, index):
        self.mags = []
        self.i = index
        self.times = []
        for ii in range(20):
            self.times.append(df['Time '][i + ii - 10])
            self.mags.append(df['Magnitude '][i + ii - 10])
        (self.mags)


data = []
list = []
i = 0
while (i < len(df['Magnitude '])):
    if (abs(df['Magnitude '][i]) <= (average - below)):
        print(df['Time '][i])
        list.append(df['Time '][i])
        data.append(data_set(i))


    i += 1

print("found values")

first_range = 0
for n in list:
    if n - first_range <= 10:
        pass
    else:
        print(n)
        first_range = n






# graphing

height = 8  # Change this for number of columns
width = math.ceil(len(list) / height)  # Change this to change the number of ROWS
fig, axes = plt.subplots(width, height, figsize=(30, 30))

row = 0
col = 0

for i in range(len(list)):
    axes[row][col].plot(list[i].times, list[i].mags)
    # axes[0][i].set_xticklabels(df['Time '], rotation=45)
    col += 1
    if (col > height - 1):
        col = 0
        row += 1

plt.show()

The output is below:

/Users/aaronhuang/.conda/envs/EXTTEst/bin/python "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py"
2456116.494
2456116.535
2456116.576
2456116.624
2456116.673
2456116.714
2456116.799
2456123.527
2456166.634
2456570.526
2456595.515
2457485.722
2457497.93
2457500.674
2457566.874
2457567.877
found values
2456116.494
2456166.634
2456570.526
2456595.515
2457485.722
2457497.93
2457566.874
Traceback (most recent call last):
  File "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py", line 65, in <module>
    axes[row][col].plot(list[i].times, list[i].mags)
AttributeError: 'numpy.float64' object has no attribute 'times'

Process finished with exit code 1

I am unsure of how to resolve the numpy.float.64. I am trying to create graphs of list which are numbers that are sorted from data. The code should print 7 graphs in total. If sharing the can data file would help please leave a comment.

1
  • When you have an AttributeError, for obj.attr, then either obj is not what you think should be, or you misread the docs for obj and are asking for the wrong attribute (or method). Most often it's the former. Here your list[i]is an element of a numpy array, which does not have times or mags attributes (whatever those are supposed to be). Commented Jul 28, 2020 at 23:20

1 Answer 1

1

Your variable list is getting individual float values from list.append(df['Time '][i]). You'll need to put your data_set objects in there instead.

Also, though it's legal Python, it's generally bad practice to name variables the same as built-ins, as you've done with list since then you'll have a hard time creating a new object of type list.

Sign up to request clarification or add additional context in comments.

2 Comments

Does this mean that I should replace all the list with data_set?
Without access to the data that you're working with or a strong sense of the problem you're looking to solve, it's tough to say. I suspect that what you have is a bit overcomplicated and should probably just be some pandas operations on a DataFrame, but I can't say for sure at a glance

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.