-1

This code

print(loc_coverage)
loc_coverage2 = loc_coverage.groupby('date').min()
print(loc_coverage2)

produces this output

date  location_coverage
0  2021-03-25              22.38
1  2021-03-25              22.38
2  2021-03-25              22.38 

            location_coverage
date                         
2021-03-25              22.38
2021-03-25              22.38

I was expecting to have one single row. Why are the date values not grouped?

The date is a datetime.date object and I have checked there are no duplicates.

Guilherme Costa
  • 308
  • 2
  • 13
  • if date is a string, that might have trailing spaces.. check that – anky Mar 25 '21 at 14:19
  • I guess some space, before – jezrael Mar 25 '21 at 14:19
  • What data type you use to store date? – Daweo Mar 25 '21 at 14:20
  • 1
    Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) ... [Discourage screenshots of code and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors)...[Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) ... [You should not post code as an image because:...](https://meta.stackoverflow.com/a/285557/2823755) – wwii Mar 25 '21 at 14:22
  • 1
    [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Mar 25 '21 at 14:22
  • 2
    So is possible test `print(loc_coverage['date'].unique())` ? – jezrael Mar 25 '21 at 14:26

2 Answers2

0
import pandas as pd
import datetime


list_value = [[datetime.date(2021, 3, 25), 22.38], [datetime.date(2021, 3, 25), 22.38], [datetime.date(2021, 3, 25), 22.38]]


df = pd.DataFrame(list_value, columns = ['date', 'loc_coverage'])

df1 = df.groupby(by = 'date')['loc_coverage'].agg(['describe'])
df2 = df.groupby(by = 'date')['loc_coverage'].agg(['min'])
df3 = df.groupby(by = 'date')['loc_coverage'].min()

print('==== Original ====')
print(df)
print()
print('==== Mode 1 ====')
print(df1)
print()
print('==== mode 2 ====')
print(df2)
print()
print('==== mode 3 ====')
print(df3)

enter image description here

Gilmar Vaz
  • 92
  • 1
  • 4
0

After doing

loc_coverage.head().to_dict()

I realized the problem is that not all the values in my date column are of the same type

{'date': {0: '2021-03-25', 1: '2021-03-25', 2: datetime.date(2021, 3, 25)},
 'location_coverage': {0: 22.38, 1: 22.38, 2: 22.38}}
Guilherme Costa
  • 308
  • 2
  • 13