lundi 28 octobre 2019

if statement not looping through all the data? [duplicate]

This question already has an answer here:

I have a CSV data that I am writing in Python. I already completed a Jupyter Notebook as a prototype. It looks like the loop is ending on the first row of data. What is the fix for this?

import numpy as np
import matplotlib.pyplot as plt 
import datetime 


data = pd.read_csv("logrecords.csv")
vehicle = 'NM52'
df = data[data['VEHICLE'] == vehicle]

degree_change = 0.000011 
speed_change = 0 
time_change = datetime.timedelta(seconds=150)

def conditionA(df, degree_change):
    lat = df['lat']
    df['lat_rolled'] = np.roll(lat, 1) 
    df['lat_difference'] = df['lat'] - df['lat_rolled']
    lat_conditionA =  df['lat_difference'] < degree_change
    lon = df['lon']
    df['lon_rolled'] = np.roll(lon, 1)
    df['lon_difference'] = df['lon'] - df['lon_rolled'] 
    lon_conditionA = df['lon_difference'] < degree_change 

    if lat_conditionA + lon_conditionA == True: 
        print(True)
    else: 
        print(False) 


def conditionB(df, speed_change):
    speed = df['speed']
    speed_rolled = np.roll(speed,1)
    df['speed_difference'] = df['speed'] - df['speed_rolled']
    speed_change_conditionB = (df['speed_difference'] == speed_change)

    if speed_change_conditionB == True:
        print(True) 
    else: 
        print(False)


def conditionC(df, time_change):
    time = df['time']
    df['time'] = pd.to_datetime(df.time)
    time_rolled = np.roll(time,1)
    df['time_rolled'] = pd.to_datetime(df.time_rolled)
    df['time_difference'] = df['time'] - df['time_rolled']
    time_conditionC = (df['time_difference'] > time_change)

    if time_conditionC == True:
        print(True) 
    else:
        print(False)


def trip_validation(conditionA, conditionB,conditionC):
    if (conditionA and conditionB and conditionC) == True: 
        print(True)
    else: 
        print(False)

    return;

trip_validation(conditionA, conditionB, conditionC)

My only output is False, which is the right answer for the first row of data.

Aucun commentaire:

Enregistrer un commentaire