Python Pandas比较DataFrame单元格中的日期时间值

问题描述 投票:0回答:1

我运行了两组速度测试并将数据记录到CSV文件中,然后我将其读回并转换为DataFrames。当我显示数据时它看起来像这样,我有2套它;一个用于测试#1,一个用于测试#2

DataFrame results table example

我想要做的是将测试#1'Time Elapsed'列的每个单元格与测试#2'Time Elapsed'列的相应单元格进行比较,并在新的DataFrame显示中以百分比比较变化(即+ 1.05%或 - 4.72%)。我不知道如何访问这些单元格并对它们进行任何比较,因为它们是奇怪的数据类型?

为了生成性能表,我编写了以下代码:

import random
import datetime as dt
import pandas as pd
import logging
import platform, psutil, GPUtil
import csv

#for debugging purposes
logging.basicConfig(filename='stressTest_LOG.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.disable(level=logging.DEBUG)

#enlarge pandas display area
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

def passGen(passLen, randSeed):
    # randSeed = None #None uses time stamp as a value
    # passLen = 15 #password length

    random.seed(a=randSeed, version=2)

    # populate lists with character ranges based of ASCII table
    letters = list(range(65, 91)) + list(range(97, 123))
    symbols = list(range(33, 48))
    numbers = list(range(48, 58))

    passCombined = letters + symbols + numbers
    random.shuffle(passCombined)

    # check if first element is from symbol list and if so replace with a number
    while passCombined[0] > 32 and passCombined[0] < 49:
        # print("First symbol: "+ str(chr(passCombined[0])))
        passCombined[0] = random.randint(48, 58)
        # print("Changed to: "+ str(chr(passCombined[0])))

    finalPassword = passCombined[slice(passLen)]

    return finalPassword


def showPass(password):
    if len(password) > 32:
        print("Invalid password length.\nHas to be less than 32 characters.")
        return -1

    print(''.join(str(chr(e)) for e in password))



####################################### Main #######################################

# Generate CSV file
with open('performanceResults2.csv', 'w', newline='') as f:

    #declare columns in CSV file and their order
    fieldnames = ['Action', 'Start Time', 'End Time', 'Time Elapsed', 'OS',
                  'System', 'RAM', 'CPU count', 'CPU freq', 'GPU']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()

    #gather system information
    info_architecture = platform.architecture()[0]
    info_machine = platform.machine()
    info_node = platform.node()
    info_system = platform.platform()
    info_os = platform.system()

    if info_os == 'Darwin':
        info_os = 'macOS'

    info_release = platform.release()
    info_version = platform.version()
    info_processor = platform.processor()
    info_pythonCompiler = platform.python_compiler()

    # get RAM memory info
    mem = psutil.virtual_memory().total
    mem = str(mem/(1024.**3)) + 'GB'

    # get CPU info
    cpu_count = psutil.cpu_count()
    cpu_freq = psutil.cpu_freq().current
    cpu_freq = round(cpu_freq / 1000, 2)
    cpu_freq = str(cpu_freq) + 'GHz'

    # get GPU info
    # Works only with Nvidia gpus and is based on nvidia-smi command
    gpuinfo = GPUtil.getGPUs()

    if len(gpuinfo) == 0:
        gpuinfo = 'Unsupported GPU model'

    #run random password generator program
    counter = 10000
    testCounter = 0


    #print("Test #1 Start time: " + str(startTime))


    for i in range(0,5):

        startTime = dt.datetime.now()

        while counter > 0:
            pass1 = passGen(30, None)
            #showPass(pass1)
            logging.debug('counter is: ' + str(counter) + ', password: ' + str(pass1))
            counter -= 1

        endTime = dt.datetime.now()
        #print("Test #1 End time  : " + str(endTime))

        timeDelta = endTime - startTime
        #print ("Test #1 Time elapsed: " + str(timeDelta))
        testCounter += 1
        counter = 10000
        testCounterDisplay = 'Test #' + str(testCounter)

        writer.writerow({'Action': testCounterDisplay, 'Start Time': startTime, 'End Time': endTime,
                         'Time Elapsed': timeDelta, 'OS': info_os, 'System': info_system, 'RAM': mem,
                         'CPU count': cpu_count, 'CPU freq': cpu_freq, 'GPU': gpuinfo})

#read back in and display the results
file = pd.read_csv('performanceResults2.csv', delimiter=',')
print(file)

为了比较结果,我只得到了这个:

import pandas as pd
import numpy as np

#enlarge pandas display area
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

#read in data to compare
test1 = pd.read_csv('performanceResults1.csv', delimiter=',')
test2 = pd.read_csv('performanceResults2.csv', delimiter=',')

#check if dataframes are equal
equality = test1.equals(test2)
print('DataFrame equal: ', equality)


df1_filtered = pd.DataFrame(test1[['Time Elapsed']])
df2_filtered = pd.DataFrame(test2['Time Elapsed'])

有什么想法吗?

python pandas dataframe datetime comparison
1个回答
0
投票

没有看到你的时间单元格格式很难提供帮助据我了解你的时间来自日期时间格式:

 dt.datetime.now()

如果你想转换为pandas时间戳:

 pd.to_datetime(dt.datetime.now())

您可以在“开始时间”和“结束时间”列上运行此操作并重新分配它们。检查你的DataFrame上的.dtypes(),它可能是“对象”,然后运行:

DF['Start Time'] = pd.to_datetime(DF['Start Time'])

在这个dtype之后应该是datetime64[ns],这将允许你进行计算。

© www.soinside.com 2019 - 2024. All rights reserved.