0

Running with this series

X = number_of_logons_all.values
split = round(len(X) / 2)
X1, X2 = X[0:split], X[split:]
mean1, mean2 = X1.mean(), X2.mean()
var1, var2 = X1.var(), X2.var()
print('mean1=%f, mean2=%f' % (mean1, mean2))
print('variance1=%f, variance2=%f' % (var1, var2))

I get:

mean1=60785.792548, mean2=61291.266868
variance1=7483553053.651829, variance2=7603208729.348722

But I wanted something like this in my PyCharm console (pulled from another result):

>>> -103 days +04:37:13.802435724...

Tried to place the np.array in a pd.Dataframe() to get the expected value by adding

.apply(pd.to_timedelta, unit='s')

...this didn't work, so I tried

new = pd.DataFrame([mean1]).to_numpy(dtype='timedelta64[ns]')

...and (still) got something like this:

>>>> [[63394]]

Anyone out there who could assist me converting to an easily comprehended datetime result from my means calculation above?

Thx, in advance for your kind support.

1 Answers1

0

You can use f-strings:

mean1, mean2 = 60785.792548, 61291.266868
variance1, variance2=7603208729.348722,7483553053.651829

print(f'mean1={pd.Timedelta(mean1, unit="s")}, mean2={pd.Timedelta(mean2, unit="s")}')
print(f'variance1={pd.Timedelta(variance1, unit="s")}, variance2={pd.Timedelta(variance2, unit="s")}')
mean1=0 days 16:53:05.792548, mean2=0 days 17:01:31.266868
variance1=88000 days 02:25:29.348722458, variance2=86615 days 04:44:13.651828766
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Great, and thx. In principle it worked, got this...mean1=0 days 17:36:34.480990274, mean2=0 days 17:39:34.165340407 variance1=93494 days 10:19:28.083414078, variance2=94031 days 03:31:31.984157562. Just like you, btw. Yet the timespread is a few months earlier this year, so not sure how I ended up with thousands of days...;o) – Hubsandspokes Dec 08 '20 at 11:39
  • @Hubsandspokes - Super! Btw, working with timedeltas is pain ;) E.g. if need custom format not easy, need [this](https://stackoverflow.com/questions/538666/format-timedelta-to-string) – jezrael Dec 08 '20 at 11:41
  • One other question: I have a number of students, i.e. 750 unique IDs, logging into an AD, some unique IDs even thousands of times from Feb to May this year. I can clarify that using a groupby and count/ID, but I have an issue trying to figure out how many times a student logs on up until a certain termination date. Say, one student ID logs on 150 times different dates during above period, but how can I put that in a Python code. E.g. I have a col named 'User Name', another one named 'Logon Time', a third one named 'Student line'. Do you have a suggestion? Thx – Hubsandspokes Dec 09 '20 at 15:53
  • @Hubsandspokes - I think new question should be nice, without data not easy answering. – jezrael Dec 10 '20 at 06:29
  • check this, https://stackoverflow.com/questions/65230004/how-do-i-add-new-column-that-adds-and-sums-counts-from-existing-column, thx for your kind support – Hubsandspokes Dec 10 '20 at 07:16