Python Forum
How to Write a function with Pandas?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Write a function with Pandas?
#1
Question 
Write a function that takes the start and end of a range returns a Pandas series object containing numbers within that range. In case the user does not pass start or end or both they should default to 1 and 10 respectively. Eg.

range_series() -> Should Return a pandas series from 1 to 10

range_series(5) -> Should Return a pandas series from 5 to 10

range_series(5, 10) -> Should Return a pandas series from 5 to 10.
Reply
#2
Learning a lot?
Reply
#3
(Jul-03-2022, 05:03 AM)deanhystad Wrote: Learning a lot?
Bruh I have exams... So practicing about it
Reply
#4
Please read: Homework and No Effort Questions
Reply
#5
First, write a failing test:

import unittest
import pandas as pd


class RangeSeriesTest(unittest.TestCase):
    def test_it_returns_a_series_from_1_to_10_by_default(self):
        actual = range_series()

        expected = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

        self.assertTrue(
            actual.equals(expected), 
            msg=f"expected: {expected}, actual: {actual}"
        )

if __name__ == '__main__':
    unittest.main()
Running it straight away gives, unsurprisingly:

Output:
$ python3 test_range_series.py E ====================================================================== ERROR: test_it_returns_a_series_from_1_to_10_by_default (__main__.RangeSeriesTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_range_series.py", line 6, in test_it_returns_a_series_from_1_to_10_by_default actual = range_series() NameError: name 'range_series' is not defined ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1)
But then we gradually fix the problems: first by introducing a function with the right name and parameters with some dummy implementation to get us past that error (hopefully!) and so on until it's working. The goal in this process is to use feedback from the tests to tell us if we're doing the right thing - if we're surprised by something, we should try and understand that (maybe we made an incorrect assumption, for example).

Writing a test before doing the implementation is about two things in my mind: focus and clarity.

Focus: even with your simple example, there are often several cases to test. You write a test for one of those at a time and because your goal is to get that test to pass, you're focussed on that one thing and can leave the others for later tests. So, it helps you work in a nice, incremental way, building up in small pieces.

Clarity: spelling out exactly what the code you're going to write is going to do means that you need to understand that and also understand how to tell if you've done it right. As an example, since I'm not a pandas expert, in writing this test I learnt that checking equality of Series with == (which is what assertEquals does) doesn't make sense and I learnt about the equals method on Series. Can you describe what you're testing in a sentence (i.e. the test name)? If not, that's probably a sign that you're trying to test too much in one go. This understanding phase also means you might think about assumptions you hold, or discover other cases you might not have thought about at first.

Having automated tests like this also means that if you break any behaviour when you're changing the code, you'll know about it because tests will start failing. Obviously, this implies you're running the tests often as you make changes.

Kent Beck's Test-Driven Development: By Example is a good introductory read on the subject. The example code is largely in Java if I remember, but the ideas are broadly applicable. Besides, there are likely other sources on the web about this anyway.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write a function with three parameters? MehmetAliKarabulut 3 2,786 Mar-04-2021, 05:23 PM
Last Post: buran
  Write function to find decryptionkey kotter 3 3,129 Dec-11-2019, 07:04 PM
Last Post: nilamo
  how to write a function in python that solves an equation samiraheen 5 9,776 Sep-28-2017, 01:10 AM
Last Post: Mekire
  Write a function sorting(L)? MoIbrahim 11 8,322 Apr-22-2017, 11:45 AM
Last Post: idontreallywolf
  function to write the contents of a webpage into a new file roadrage 4 6,042 Dec-01-2016, 09:28 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020