1

While asking another question, I struggled to paste a toy DataFrame here due to the columns being pd.datetime.date and pd.datetime.time. I would like for people answering to being able to import a toy df - which is still slightly bigger than the toy of a toy df below - quickly.

I have read this and another post which explains to use df.head().to_dict() and copy the dataframe. I did a toy example df and tried the method. This is the result of df.head().to_dict():

In [0]: df = pd.DataFrame({'location':['Silvretta'], 'date':[pd.to_datetime('2022-03-30 07:35:00').date()], 'time':[pd.to_datetime('2022-03-30 07:35:00').time()]})
In [1]: s = df.head().to_dict()
In [2]: print(s)
Out [2] 
{'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}}

Now when I call pd.DataFrame(s) I obtain my dataframe, but when I manually copy the string (like the people helping me would have to do) I get the following error.

In [3] pd.DataFrame(s) # works
In [4] pd.DataFrame({'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}})
Out [4] 
---------------------------------------------------------------------------
[...]
NameError: name 'datetime' is not defined

Firstly, I do not get why there is a difference between the two options. Why does it make a difference? Finally, how do I nicely format and display a DataFrame with datetimes to get help? The In [0] is obviously oversimplified, so I would not like to 'build' my df in the next question, but just paste the final copyable version.

Thanks for the help!

I have tried to show the table by formatting a table using the StackOverflow table, but that is quite time consuming, even for a 5x6 matrix (is there a trick?) and not copyable (I think). The sharing using a dictionary does not work for me. I could post the whole way of constructing the df, but I do not want to overload the next question I am going to ask.

1 Answers1

1

If need create new DataFrame with dict use import datetime for correct import datetimes variables:

import datetime

print (pd.DataFrame({'location': {0: 'Silvretta'}, 
                     'date': {0: datetime.date(2022, 3, 30)}, 
                     'time': {0: datetime.time(7, 35)}}))
    location        date      time
0  Silvretta  2022-03-30  07:35:00
    

Explanantion:

If pass pd.DataFrame(s) there is not parsed printed version of dict, so working well, else for parsing string representation of datetimes need import datetime.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks, sometimes it is so obvious. But why does it make a difference whether I call `pd.DataFrame` using `s` or the dictionary? – Firefighting Physicist Apr 01 '22 at 06:50
  • @firefighting_physicist - because if call `print` then get reperesentation of dictionary. So `datetime`s are printed with `datetime.date`, `datetime.time` for possible see it is type `datetime`. Because it not working `pd.DataFrame(print(s))`. – jezrael Apr 01 '22 at 06:54
  • `pd.DataFrame(print(s))` should never work because print does not return anything, but why does `pd.DataFrame(s)` work without importing datetime while `pd.DataFrame({'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}})` fails? – Firefighting Physicist Apr 01 '22 at 06:59
  • @firefighting_physicist - because `print` only show ouput to screen. And `pd.DataFrame(s)` not call `print`, it is passed dictionary. – jezrael Apr 01 '22 at 07:02
  • Sorry, I still do not get it. Why does `pd.DataFrame(s)` with `s` being `{'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}}` work, while `pd.DataFrame({'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}})` fails? – Firefighting Physicist Apr 01 '22 at 07:07
  • @firefighting_physicist - `pd.DataFrame({'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}})` working if call `import datetime` because you need parse printed version of dictioanry. If pass `pd.DataFrame(s)` there is not parsed printed version of dict, so working well. – jezrael Apr 01 '22 at 07:10
  • I think I got it. `s` is NOT `{'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}}`, only the visualization generated by `print(s)` is `{...}`. Therefore, I cannot use `{...}` instead of `s`, because it is not `s` but just its parsed representation. Thanks! – Firefighting Physicist Apr 01 '22 at 07:13