0

enter image description herei have a csv file like

Slot                   object
Name                   object
Plate                  object
Date_of_reg    datetime64[ns]
dtype: object

I was working on a parking program that can give person data based on given date I took date from user as shown below:

inputDate =input("Enter the date in format 'dd-mm-yy':")
y=datetime.datetime.strptime(inputDate,'%d-%m-%Y')
c=np.datetime64(y)

now if i am trying to read and print data from my csv file i get empty columns even though i have date

with open('parking.csv', mode='r') as park:
    park_reader =csv.DictReader(park)
    for row in park_reader:

                    


                     //some code that didn't work\\

I dunno what to do next any help would be hugely appreciated :) remember i only want to print rows where the input date matches

2 Answers2

2

The empty database was being created as np.datetime64 is no longer comparable to datetime.datetime so that waa the problem

0

Try the following

import pandas as pd
import datetime as dt

df = pd.read_csv('parking.csv')
df['Date_of_reg'] = pd.to_datetime(df['Date_of_reg'])

input_date = input("Enter the date in format 'dd-mm-yy':")
input_date = dt.datetime.strptime(input_date, '%d-%m-%y')  # NB: non-capitalized 'y' if you want year without century padding

filtered_df = df[df['Date_of_reg'] == input_date]
print(filtered_df)

oskros
  • 3,101
  • 2
  • 9
  • 28
  • the above code gave me keyError : 'Date_of_reg' dont know what might be causing it – DOMINATOR X Nov 13 '20 at 10:34
  • Then your column is probably called something else than 'Date_of_reg' - Try `print(df.columns)` to see your column names – oskros Nov 13 '20 at 10:35
  • Columns: [Slot, Name, Plate, Date_of_reg] Index: [] – DOMINATOR X Nov 13 '20 at 10:45
  • here's what i got – DOMINATOR X Nov 13 '20 at 10:46
  • Ok not really sure what is going on without seeing what your data looks like – oskros Nov 13 '20 at 10:57
  • Either that or make a small example of your data that reproduces the error https://stackoverflow.com/help/minimal-reproducible-example – oskros Nov 13 '20 at 11:03
  • Thats empty? It only seems to have one line and no column headers. Also, please open it in notepad, not in excel. – oskros Nov 13 '20 at 11:30
  • so i sorted everything that is header now exists and my data wont override it but still the same problem even after using the code that you suggested.. – DOMINATOR X Nov 13 '20 at 17:41
  • I sorted it all out and THANKS A LOT BUDDY I MEAN IT and sorry for being ANNOYING the empty dataframe was being created as numpy.datetime64 is no longer comparable to datetime.datetime object so i converted datetime to np.datetime64 THANKS AGAIN :)) – DOMINATOR X Nov 13 '20 at 18:46